Jump to content

Currency: Difference between revisions

2,811 bytes added ,  3 years ago
Added AppleScript.
m (→‎{{header|Ruby}}: Updated output)
(Added AppleScript.)
Line 110:
total: 23683000000000006.16
</pre>
 
=={{header|AppleScript}}==
 
The AppleScript core language doesn't recognise currency values specifically and its numbers don't have the precision required for this task, but through ASObjC, it's able to use Foundation classes which do.
 
<lang applescript>use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
 
-- Derive an NSDecimalNumber from an AppleScript number or numeric text.
-- NSDecimalNumbers also allow arithmetic and have a far greater range than AS numbers.
on decimalNumberFrom(n)
return current application's class "NSDecimalNumber"'s decimalNumberWithString:(n as text)
end decimalNumberFrom
 
-- Multiply two NSDecimalNumbers.
on multiply(dn1, dn2)
return dn1's decimalNumberByMultiplyingBy:(dn2)
end multiply
 
-- Add two NSDecimalNumbers.
on add(dn1, dn2)
return dn1's decimalNumberByAdding:(dn2)
end add
 
on billTotal(quantitiesAndPrices, taxRate, currencySymbol)
-- Set up an NSNumberFormatter for converting between currency strings and NSDecimalNumbers.
set currencyFormatter to current application's class "NSNumberFormatter"'s new()
tell currencyFormatter to setNumberStyle:(current application's NSNumberFormatterCurrencyStyle)
tell currencyFormatter to setCurrencySymbol:(currencySymbol)
tell currencyFormatter to setGeneratesDecimalNumbers:(true)
-- Tot up the bill from the list of quantities (numbers or numeric strings) and unit prices (currency strings with symbols).
set subtotal to decimalNumberFrom(0) -- or: current application's class "NSDecimalNumber"'s zero()
repeat with thisEntry in quantitiesAndPrices
set {quantity:quantity, unitPrice:unitPrice} to thisEntry
set entryTotal to multiply(decimalNumberFrom(quantity), currencyFormatter's numberFromString:(unitPrice))
set subtotal to add(subtotal, entryTotal)
end repeat
-- Work out the tax and add it to the subtotal.
set tax to multiply(subtotal, decimalNumberFrom(taxRate / 100))
set total to add(subtotal, tax)
-- Format and return the results.
return (current application's class "NSString"'s stringWithFormat_("Subtotal: %@
Tax: %@
Total: %@", ¬
currencyFormatter's stringFromNumber:(subtotal), ¬
currencyFormatter's stringFromNumber:(tax), ¬
currencyFormatter's stringFromNumber:(total))) ¬
as text
end billTotal
 
-- Demo code:
set currencySymbol to "$"
set quantitiesAndPrices to {{quantity:"4000000000000000", unitPrice:currencySymbol & "5.50"}, ¬
{quantity:2, unitPrice:currencySymbol & 2.86}}
set taxRate to 7.65
return billTotal(quantitiesAndPrices, taxRate, currencySymbol)</lang>
 
{{output}}
<lang applescript>"Subtotal: $22,000,000,000,000,005.72
Tax: $1,683,000,000,000,000.44
Total: $23,683,000,000,000,006.16"</lang>
 
=={{header|AWK}}==
557

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.