Re: rounding to decimal place
Re: rounding to decimal place
- Subject: Re: rounding to decimal place
- From: "Gary (Lists)" <email@hidden>
- Date: Sun, 09 Oct 2005 15:53:16 -0400
"Wayne Melrose" wrote:
> On Oct 9, 2005, at 8:50 PM, Doug McNutt wrote:
>
>>
>> The classic procedure is:
>>
>> 1) multiply by 1000
>> 2) add 0.5
>> 3) truncate to an integer
>> 4) divide by 1000
>>
>> But step 4 may introduce an "error" because 1/1000 is not exactly
>> expressible as a floating point binary number. Figuring out how to
>> tell AppleScript to print in a three decimal digit format is beyond
>> my mental capacity. Perhaps inserting the decimal point into the
>> character string that represents the integer at step 3.
>
> ah ha! ...
>
> I think I'll use that til something goes wrong..
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
--
Gary
_______________________________________________
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