Formatting the display of an extended var


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)
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ShowMessage(Format('%8.4f', [329.3654735]))


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike LittlewoodEngineerCommented:
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;
   eValue: extended;
   eValue := pValue * power(10, pDP);
   eValue := roundUp( eValue);
   result := eValue / power(10, pDP);

function roundUp( const pValue: extended): int64;
   result := trunc( pValue) + trunc (frac( pValue) * 2);
abrakadabra1Author Commented:
ok. that's the second version. anything for the first ?
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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 ?)
Mike LittlewoodEngineerCommented:
This works fine for both

   d: double;
   s: string;
   s := '329.3654735';
   //s := '329.36';
   d := strToFloat( s);
   d := roundDP( d, 4);
   showMessage( format( '%.4f', [ d]));
abrakadabra1Author Commented:
thank you for your help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.