Formatting the display of an extended var

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)
abrakadabra1Asked:
Who is Participating?
 
ziolkoConnect With a Mentor Commented:
ShowMessage(Format('%8.4f', [329.3654735]))

ziolko.
0
 
mikelittlewoodConnect With a Mentor Commented:
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
 
abrakadabra1Author Commented:
ok. that's the second version. anything for the first ?
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
abrakadabra1Author Commented:
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
 
mikelittlewoodConnect With a Mentor Commented:
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
 
abrakadabra1Author Commented:
thank you for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.