Reference · Decimals and money
money.zero
money.zero(str) -> map | error(str)What it does
Nothing of a currency: { amount: 0.00, currency: "USD" }, the zero written with the
currency's minor units (0 for JPY, 0.000 for KWD). It is the start of a running
total and the default for an amount that was not given. An unknown currency is an
error value.
Examples
print(money.zero("USD"))print(money.zero("JPY"), money.zero("KWD"))print(money.add(money.zero("USD"), money.of("2.50", "USD"))){ amount: 0.00m, currency: "USD" }{ amount: 0m, currency: "JPY" } { amount: 0.000m, currency: "KWD" }{ amount: 2.50m, currency: "USD" }Edge cases
- The code's case does not matter; the result has it in upper case.
- An unknown code is an error value, as with money.of.
money.is_zerois true of it, andmoney.formatwrites it as$0.00.
print(money.zero("eur"), money.zero("XYZ"))print(money.is_zero(money.zero("USD")), money.format(money.zero("USD"))){ amount: 0.00m, currency: "EUR" } err("unknown currency \"XYZ\"")true $0.00Errors
A currency that is not text is a runtime error; text that is not a currency is an error value.
print(money.zero(840))line 1: `money.zero` expects a currency code (a str) as argument 1, got int 840print(is_err(money.zero("XYZ")), money.zero("XYZ").error)true unknown currency "XYZ"Chaining
As the first value of a reduce it starts a total; after ?? it fills in a missing
amount:
let lines = [money.of("9.99", "USD"), money.of("0.01", "USD")]print(lines | reduce(money.zero("USD"), money.add))print(money.of("x", "USD") ?? money.zero("USD")){ amount: 10.00m, currency: "USD" }{ amount: 0.00m, currency: "USD" }See also
money.is_zero, money.of, money.add, money.minor_units
Thread 0 posts
On money.zero(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo