Re: rounding to decimal place
Re: rounding to decimal place
- Subject: Re: rounding to decimal place
- From: "Nigel Garvey" <email@hidden>
- Date: Mon, 10 Oct 2005 12:56:33 +0100
"Gary (Lists)" wrote on Sun, 09 Oct 2005 15:53:16 -0400:
>Here is Nigel Garvey's rounding handler, which I've used for years,
>without ever worrying about it.
>
>
>roundThis(75.5436, 2) --> 75.54
>
>on roundThis(n, numDecimals)
> set x to 10 ^ numDecimals
> (((n * x) + 0.5) div 1) / x
>end roundThis
Did I write that? Goodness. It must have been some time ago. I haven't
used '(n + 0.5) div 1' to round for years. I use 'n div 0.5 - n div 1',
which works for negatives too.
on roundThis(n, numDecimals)
set x to 10 ^ numDecimals
tell n * x to return (it div 0.5 - it div 1) / x
end roundThis
As Doug mentioned, this sort of calculation is occasionally subject to
floating point errors. The incidence of these errors can be reduced (but
not eliminated) by the hack of coercing to string and back:
on roundThis(n, numDecimals)
set x to 10 ^ numDecimals
tell (n * x) as string as number to return (it div 0.5 - it div 1) / x
end roundThis
For those who prefer equidistant values to round to the nearest even:
on roundThisIEEE(n, numDecimals)
set x to 10 ^ numDecimals
tell (n * x) as string as number
if ((it mod 2) ^ 2 > 0.25) then
(it div 0.5 - it div 1) / x
else
(it div 1) / x
end if
end tell
end roundThisIEEE
NG
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden