We help IT Professionals succeed at work.

# Reduce decimal places in extended var

on
583 Views
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

## View Solution Only

Commented:
uses Math;

var
a: double;
begin
If Frac(x) <> 0 then
begin
If x >= 0 then
else
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

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

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.
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)

Commented:
you can also use (trunc(v*10000))/10000

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

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

Commented:
Thanks works good
Unlock the solution to this question.

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.