Re: Limiting Decimal Places in a Floating Variable?
Re: Limiting Decimal Places in a Floating Variable?
- Subject: Re: Limiting Decimal Places in a Floating Variable?
- From: "Marc K. Myers" <email@hidden>
- Date: Wed, 22 Nov 2000 11:57:19 -0500
- Organization: [very little]
Jonathan Simms wrote:
>
Date: Wed, 22 Nov 2000 08:24:21 -0500
>
Subject: Limiting Decimal Places in a Floating Variable?
>
From: Jonathan Simms <email@hidden>
>
To: <email@hidden>
>
>
I'm trying to come up with a running balance for a list of numbers (like a
>
check book).
>
The list of numbers are all to 2 decimal places. What's bizarre is that
>
applescript does fine (adds to the correct number of places) and then
>
returns values like -2134.2399999 for no apparent reason
>
>
The nines don't really bother me, as all of the numbers to the left of the
>
nines are correct, I just want to know how to limit the number to 2 decimal
>
places.
>
>
(although it is kind of interesting considering that there is an error being
>
added somewhere along the line...would a comma make a difference i.e.
>
>
is 9000.00 different from 9,000.00 ?)
By putting in a decimal point you automatically put yourself into the
realm of "real" (as opposed to "integer") arithmetic, which can result
in the weirdness you're experiencing. There are two ways (that I know
of) to get around it. The first is to multiply all your numbers by 100
and coerce them into integers:
set realValue to 9000.00
set intValue to (realValue * 100) div 1
and then divide by 100 before you work with the result of adding up the
column. The other is to use the "round" function on the result:
set theTotal to (round(theTotal * 100)) / 100
This would change -2134.2399999 to -2134.24, which, you say, is not the
value you expected.
AppleScript itself doesn't deal with formatted numbers, so you can't use
commas. There are scripting additions that format numbers, which is
probably what you want. Otherwise, 9000.00 will be represented as 9000.
Marc [11/22/00 11:57:05 AM]