Solved

Formatting the display of an extended var

Posted on 2008-11-17
Medium Priority
189 Views
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
• 3
• 2

LVL 21

Accepted Solution

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

ziolko.
0

LVL 15

Assisted Solution

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

ID: 22974301
ok. that's the second version. anything for the first ?
0

Author Comment

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

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

ID: 22976048
0

Featured Post

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, …