Reference · Decimals and money

money.zero

signature
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")))
output
{ amount: 0.00m, currency: "USD" }{ amount: 0m, currency: "JPY" } { amount: 0.000m, currency: "KWD" }{ amount: 2.50m, currency: "USD" }

Edge cases

print(money.zero("eur"), money.zero("XYZ"))print(money.is_zero(money.zero("USD")), money.format(money.zero("USD")))
output
{ amount: 0.00m, currency: "EUR" } err("unknown currency \"XYZ\"")true $0.00

Errors

A currency that is not text is a runtime error; text that is not a currency is an error value.

this one fails
print(money.zero(840))
output
line 1: `money.zero` expects a currency code (a str) as argument 1, got int 840
print(is_err(money.zero("XYZ")), money.zero("XYZ").error)
output
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"))
output
{ amount: 10.00m, currency: "USD" }{ amount: 0.00m, currency: "USD" }

See also

money.is_zero, money.of, money.add, money.minor_units

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

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