Reference · Decimals and money

money.minor_units

signature
money.minor_units(str) -> int | error(str)

What it does

How many decimals a currency has, from ISO 4217: 2 for USD and EUR (cents), 0 for JPY and KRW, 3 for KWD and BHD (fils). It is what money.of checks an amount against and what money.to_minor counts in. A code that is not a currency is an error value.

Examples

print(money.minor_units("USD"), money.minor_units("JPY"), money.minor_units("KWD"))print(money.minor_units("EUR"), money.minor_units("BHD"))
output
2 0 32 3

Edge cases

print(money.minor_units("eur"), money.minor_units(" usd "))print(money.minor_units("XYZ"), money.minor_units("XAU"))
output
2 2err("unknown currency \"XYZ\"") err("unknown currency \"XAU\"")

Errors

A code that is not text is a runtime error; text that is not a currency is an error value, checked with is_err or given a default with ??.

this one fails
print(money.minor_units(null))
output
line 1: `money.minor_units` expects a currency code (a str) as argument 1, got null null
print(is_err(money.minor_units("XYZ")), money.minor_units("XYZ") ?? 2)
output
true 2

Chaining

print("JPY" | money.minor_units)print(["USD", "JPY", "KWD"] | map(money.minor_units))let known = ["USD", "XYZ", "EUR"] | filter((c) => !is_err(money.minor_units(c)))print(known)
output
0[2, 0, 3]["USD", "EUR"]

See also

money.of, money.to_minor, money.from_minor, money.zero

Thread
7Useful? Like it, or add to its thread below.

Thread 0 posts

On money.minor_units(): 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