Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
53
Task/Currency/AppleScript/currency-1.applescript
Normal file
53
Task/Currency/AppleScript/currency-1.applescript
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
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)
|
||||
3
Task/Currency/AppleScript/currency-2.applescript
Normal file
3
Task/Currency/AppleScript/currency-2.applescript
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
"Subtotal: $22,000,000,000,000,005.72
|
||||
Tax: $1,683,000,000,000,000.44
|
||||
Total: $23,683,000,000,000,006.16"
|
||||
Loading…
Add table
Add a link
Reference in a new issue