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.
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.
ASKER
hmm interesting, I will give that a speed profile and we'll see how it goes ;-)
ASKER
it seems to always want to return 0.
showmessage(FloatToStr(dmR ound(1.212 121222222, 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.
showmessage(FloatToStr(dmR
= 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you can also use (trunc(v*10000))/10000
ASKER
yeap, the question title says it all
".... extended var"
;-)
Thanks for the reply, I will give this a shot in a minute
".... extended var"
;-)
Thanks for the reply, I will give this a shot in a minute
ASKER
Thanks works good
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,ADi
else
result := RoundTo(x-0.0000000001,ADi
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