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?

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

x
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.

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

ziolko.
0

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;
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
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 ?)
0
Mike LittlewoodEngineerCommented:
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
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
Delphi

From novice to tech pro — start learning today.