Reference · Decimals and money
money.to_minor
money.to_minor(map) -> intWhat it does
The amount as an int of its currency's minor units: cents for USD, yen for JPY, fils
for KWD. That int is what to keep in an integer column (amount_cents integer) when the
database has no exact decimals, and what to send to a payment API that counts in cents.
money.from_minor turns it back.
Examples
print(money.to_minor(money.of("12.50", "USD")))print(money.to_minor(money.of(1250, "JPY")), money.to_minor(money.of("1.234", "KWD")))print(money.to_minor(money.of("-0.01", "USD")))12501250 1234-1Edge cases
- The map need not come from
money.of: any{ amount, currency }whose amount is a decimal, an int or numeric text (as JSON brings it) is money, read asmoney.ofreads an amount: the code trimmed and in any case, text without an exponent. - An amount made by hand with more decimals than the currency has is a runtime error,
not rounded:
2.125dollars is not a count of cents. Round it first withmoney.round, which says how:2.125half to even is 212 cents,2.135is 214. - Keys beyond
amountandcurrencyare ignored.
print(money.to_minor({ amount: dec("1.5"), currency: "USD" }), money.to_minor({ amount: 3, currency: " usd " }))print(money.to_minor(money.round({ amount: "2.125", currency: "USD" })), money.to_minor(money.round({ amount: "2.135", currency: "USD" })))let m = json.parse(`{ "amount": "12.50", "currency": "USD", "note": "x" }`)print(money.to_minor(m))150 300212 2141250Errors
A value that is not money is a runtime error, not an error value: no currency, an
unknown one, an amount that is a float, or no map at all. The message says what was
given and why it is not money. An amount with more decimals than the currency has, or
whose minor units do not fit an int, is a runtime error too, and the message names the
amount; money.of refuses such an amount earlier, as an error value, so it arises only
from a map made by hand.
print(money.to_minor({ amount: 1.5, currency: "USD" }))line 1: `money.to_minor` expects money ({ amount, currency }) as argument 1, got map { amount: 1.5, currency: "USD" }: its amount is a float: make it with money.of(dec(x), currency)print(money.to_minor({ amount: dec("99999999999999999999"), currency: "USD" }))line 1: `money.to_minor`: 99999999999999999999 USD is too large an amountprint(money.to_minor({ amount: "2.125", currency: "USD" }))line 1: `money.to_minor`: 2.125 has more decimals than USD has (2): round it first with money.round(m, mode)Chaining
The amount comes first in a pipeline, so money.to_minor closes one:
print("19.99" | money.of("USD") | money.to_minor)print(money.allocate(money.of(100, "USD"), 3) | map(money.to_minor))print(money.allocate(money.of(100, "USD"), 3) | map(money.to_minor) | sum)1999[3334, 3333, 3333]10000See also
money.from_minor, money.of, money.minor_units, money.round, int
Thread 0 posts
On money.to_minor(): 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