We help IT Professionals succeed at work.

Reduce decimal places in extended var

wildzero
wildzero asked
on
583 Views
Last Modified: 2010-04-21
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.
Comment
Watch Question

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

Author

Commented:
hmm interesting, I will give that a speed profile and we'll see how it goes ;-)

Author

Commented:
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.
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
you can also use (trunc(v*10000))/10000

Author

Commented:
yeap, the question title says it all
".... extended var"

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

Author

Commented:
Thanks works good
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.