Link to home
Start Free TrialLog in
Avatar of wildzero
wildzero

asked on

Reduce decimal places in extended var

Hi there,

I got a variable which contains anything from 0 to 0.xxxxxxxxxxxxxxxxx
Is there someway I can limit the it to 5 decimal places?

The hack way I can think of is to convert it to a string, set the length then convert it back, but thats ugggly.
Avatar of dundeemedia
dundeemedia

uses Math;

Function dmRound(x:Double;const ADigit: TRoundToRange):Double;
var
a: double;
begin
If Frac(x) <> 0 then
   begin
      If x >= 0 then
         result := RoundTo(x+0.0000000001,ADigit)
      else
         result := RoundTo(x-0.0000000001,ADigit);
   end else result := x;
end;

You'd use it as:

dmRound(1.12345678,5) = 1.12346

The +- 0000000001 ensures that there are no false results due to the way that doubles are stored, ie, 1.43 could actually be stored as 1.4299999999, so at 5 decimal places you would get 1.42999
Avatar of wildzero

ASKER

hmm interesting, I will give that a speed profile and we'll see how it goes ;-)
it seems to always want to return 0.

showmessage(FloatToStr(dmRound(1.212121222222,5)));
= 0

Usually I wouldn't display this as a string, but just want to check as it was also returning 0 in other places in my code.
ASKER CERTIFIED SOLUTION
Avatar of dundeemedia
dundeemedia

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you can also use (trunc(v*10000))/10000
yeap, the question title says it all
".... extended var"

;-)
Thanks for the reply, I will give this a shot in a minute
Thanks works good