Re: Simple calculation
Re: Simple calculation
- Subject: Re: Simple calculation
- From: Paul Berkowitz <email@hidden>
- Date: Fri, 25 Jan 2002 09:43:10 -0800
On 1/25/02 4:08 AM, "Rob Stott" <email@hidden> wrote:
>
Heeeelp!
>
>
Part of my script involves multiplying two figures using the simple lines;
>
>
set theCost to "#" & (327.75 * 50)
>
>
...I get the result 1.63875E+4 which is all well and good but the script
>
forwards the figure to our accountant who is foxed by figures that aren't in
>
the format #16387.50
>
>
Is there a way to do this? (Short of getting a smarter accountant...)
>
If the amount is always entered as a number with a decimal point, even for
whole amounts (like 327.00, not 327), then this will do it:
"#" & ((amt * 50) div 1) & text 2 thru 4 of ("" & ((amt * 50) mod 1) & "0")
so if amt = 327.75, the result is "#16387.50". If amt = 327.00, the result
is #16350.00.
If the amt could be entered as 327 (no decimal) or with decimal then just
add 'as real':
#" & (((amt as real) * 50) div 1) & text 2 thru 4 of ("" & (((amt as real) *
50) mod 1) & "0")
To make it as general as possible:
to CalculateTotal(amt, num)
return "#" & (((amt as real) * num) div 1) & text 2 thru 4 of ("" &
(((amt as real) * num) mod 1) & "0")
end CalculateTotal
CalculateTotal(1215.56, 1000)
--> #1215560.00
Do you need commas too?
to CalculateTotal(amt, num)
set roundNum to "" & (((amt as real) * num) div 1)
set n to (count roundNum) / 3
set x to 4
repeat while n > 1
set firstBit to text 1 thru -x of roundNum
set lastBit to text -(x - 1) thru -1 of roundNum
set roundNum to firstBit & "," & lastBit
set x to x + 4
set n to (count firstBit) / 3
end repeat
return "#" & roundNum & text 2 thru 4 of ("" & (((amt as real) * num)
mod 1) & "0")
end CalculateTotal
CalculateTotal(1215.56, 1000)
--> "#1,215,560.00"
CalculateTotal(12, 50)
--> "#600.00"
I imagine that the "#" might really be a British pound sign?
--
Paul Berkowitz