Re: Currency Numbers
Re: Currency Numbers
- Subject: Re: Currency Numbers
- From: Paul Berkowitz <email@hidden>
- Date: Wed, 21 Jun 2006 20:38:13 -0700
- Thread-topic: Currency Numbers
On 6/21/06 7:26 PM, "kai" <email@hidden> wrote:
>
> On 22 Jun 2006, at 03:10, Nigel Garvey wrote:
>
>> on currency_format for v
>> (v div 1 as text) & (0.0 as text)'s character 2 & ((((v mod 1
>> as text) * 100) ^ 2) ^ 0.5 + 0.5) div 1
>> end currency_format
>>
>> currency_format for -321.255
>
> Of course. Excellent, Mr. G! :-D
>
> (I only hope nobody developed a crick in their neck, watching this
> being batted back and forth so... ;-))
Just to be awkward, here's a new twist. I can't recall how far back booleans
can be coerced to integers (Panther?), but here's an alternative to
(y ^ 2) ^ 0.5
to get the absolute value of a number. Just for the hell of it. ;-)
(-((y < 0) as integer) * y) + (((y > 0) as integer) * y)
There's no particular reason to do it this way, of course. OMM repeating the
square/root method 10,000 times takes 3 ticks, while my boolean coercion
method takes 15 ticks. So it's slower, but who would ever know? (12 ticks
longer = 1/5th of a second. Over 10,000 loops, that's pretty good.)
In order to avoid that cumbersome expression 4 times over for ((v mod 1 as
text) * 100) instead of y, I'd set a variable:
on currency_format for v
set y to ((v mod 1 as text) * 100)
(v div 1 as text) & (0.0 as text)'s character 2 & ((-((y < 0) as
integer) * y) + (((y > 0) as integer) * y) + 0.5) div 1
end currency_format
currency_format for -321.255
--> -321.26
--
Paul Berkowitz
_______________________________________________
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