?
Solved

Formatting the display of an extended var

Posted on 2008-11-17
6
Medium Priority
?
189 Views
Last Modified: 2012-05-05
Hi

I need to format an extended variable like this:
- if it contains a value like this: 329,3654735 then the formatting should cut it to: 329,3654
- if it contains a value like this: 329,36 then it should be formatted to: 329,36 (and second version: 329,3600)
0
Comment
Question by:abrakadabra1
  • 3
  • 2
6 Comments
 
LVL 21

Accepted Solution

by:
ziolko earned 80 total points
ID: 22974194
ShowMessage(Format('%8.4f', [329.3654735]))

ziolko.
0
 
LVL 15

Assisted Solution

by:mikelittlewood
mikelittlewood earned 160 total points
ID: 22974299
Be aware of any automatic rounding by delphi though, it uses bankers rounding, not true rounding.
If you want to do your own rounding first before formatting, use these:

// round to x no. of decimal places
function roundDP( const pValue: extended; const pDP: word = 0): extended;
var
   eValue: extended;
begin
   eValue := pValue * power(10, pDP);
   eValue := roundUp( eValue);
   result := eValue / power(10, pDP);
end;

function roundUp( const pValue: extended): int64;
begin
   result := trunc( pValue) + trunc (frac( pValue) * 2);
end;
0
 

Author Comment

by:abrakadabra1
ID: 22974301
ok. that's the second version. anything for the first ?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:abrakadabra1
ID: 22974320
and also, can the format be used without the 8 number ? i don't need those spaces in front (and what if the size of the var before the decimal exceeds 8 ?)
0
 
LVL 15

Assisted Solution

by:mikelittlewood
mikelittlewood earned 160 total points
ID: 22974375
This works fine for both

var
   d: double;
   s: string;
begin
   s := '329.3654735';
   //s := '329.36';
   d := strToFloat( s);
   d := roundDP( d, 4);
   showMessage( format( '%.4f', [ d]));
0
 

Author Comment

by:abrakadabra1
ID: 22976048
thank you for your help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question