Re: Currency Numbers
Re: Currency Numbers
- Subject: Re: Currency Numbers
- From: "Gary (Lists)" <email@hidden>
- Date: Mon, 19 Jun 2006 14:09:59 -0400
"Wayne Melrose" wrote:
>
> On Jun 19, 2006, at 4:55 PM, David Bradley wrote:
>
>> Hi,
>>
>> Can you format numbers to 2 decimal points in Applescript. I want
>> to format the numbers in to a currency format
>
> David,
>
> this is a handler that someone posted once before when I asked the same
> question...
>
> Sorry whoever that was ... otherwise I would give you the credit for it!
>
> --- used for rounding number
> on roundThis(n, numDecimals)
> set x to 10 ^ numDecimals
> (((n * x) + 0.5) div 1) / x
> end roundThis
This is a nice historical call-back and an excellent demonstration of the
continuity of community. Despite all the bumps and grinds of years, and all
the small or harmless or unintentional offenses that we inevitably carom,
without ill-will, from one author to another, there is evidence of community
and of the collaborative nature of learning.
Little trips through time like this help to remind me of the great value of
this resource, of the diversity of the participants, and of the real
importance of building relationships that are able to endure time and
change.
Wayne, this may be nostalgic for you. ;)
> Thread started at 08:33 +0200 10/9/05, when Wayne Melrose wrote:
>>>
>>> what is the best way to round numbers to the nearest decimal place?
>>>
>>> 1.123945565
>>> to
>>> 1.124
So, where does this handler come from?
This is the work of one Nigel Garvey. I re-posted his script [as did Stefan
Eriksson] in reply to Wayne's '05 post, and we all then had the opportunity
to take advantage of some revisions Nigel made in response. (It's worth
noting that I was re-cycling the code from several years prior to '05. I
wonder how long Nigel's handler has circulated...? Maybe I can find the
original 'Q' source.)
Here is the original October '05 exchange including the revision Nigel
offered, a nice integration of a floating point heads-up [1] from resident
math expert Doug [McNutt], and a Bonus Handler from Nigel for IEEE rounding.
A continued stroll through 2005's rounding thread would not be complete
without some input from Emmanuel (who then tried out a Smile marketing
slogan that didn't really stick ;). [2]
It's well worth keeping this NG post around, IMO.
Thread started at 08:33 +0200 10/9/05, when Wayne Melrose wrote:
>>
>> what is the best way to round numbers to the nearest decimal place?
>>
>> 1.123945565
>> to
>> 1.124
On 10 October 2005, Nigel Garvey wrote:
> "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
(Next, Nigel refers to an earlier post in the thread, and its author Doug
McNutt. [1])
> 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
[1] The note from Doug:
On 9 October 2005, 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.
[2] The offering by Emmanuel:
> Hi Wayne, one of several questions you wouldn't ask if you worked in
> Smile, the professional environment you really expect when you think
> of AppleScript. That would be format x into "######.000", for
> instance.
>
> Emmanuel
Note:
I like the test slogan's message, but I think it has too many words for an
American audience. Discarded also because of its cultural context, there was
once a suggestion:
Smile. It even mows your lawn.
and also the pathetic:
Smile. AppleScript Editor of the Swiss Army.
and the only slightly better:
Smile. The Swiss Army Knife of AppleScript.
Now, with years of reflection, I might suggest:
Smile. It's AppleScript.
Or, with a slightly different tone and meaning:
Smile. It is AppleScript.
For the Enlightened Baby-Boomer Zen-on-the-Go Urban Professional
AppleScripter, it may be worth testing this one:
Smile. It will make your day.
;)
_______________________________________________
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