Re: Rounding real numbers
Re: Rounding real numbers
- Subject: Re: Rounding real numbers
- From: Nigel Garvey <email@hidden>
- Date: Mon, 29 Jan 2001 02:00:34 +0000
giZm0 wrote on Sun, 28 Jan 2001 16:32:06 -0500:
>
Hi,
>
i am uing a value qich is representing the amount of bytes of a specific
>
file. To get the number in megabytes instead, i worte this:
>
>
(thenumber / 1024) / 1024
>
>
Now, when i dialog this number it contains about 10 numbers after the point
>
(.). For example : 6.022548445
>
While i know this is normal, i would like to round this number to two
>
decimals only (6.02). Does anyone knows an easy way to do this ?
>
>
I could also delete the other decimals, without rounding... btu i dont know
>
how
Multiply by 10 ^ (required number of decimal places) to shift the number
that many places to the left (602.2548445), round off or truncate the
result, then divide again by 10 ^ (number of decimal places) to shift
what's left back to its original position.
on trnc of n at d given rounding:rounding
tell 10 ^ d
tell n * it -- ie. n * (10 ^ d)
if rounding then
it div 0.5 - it div 1 -- round to nearest
else
it div 1 -- or truncate
end if
end tell
result / it -- return the result divided by (10 ^ d)
end tell
end trnc
trnc of 6.022548445 at 2 with rounding
--> 6.02
NG