Reference · Decimals and money
money.is_zero
signature
money.is_zero(map) -> boolWhat it does
Whether an amount is nothing: true for zero in any currency, false for any other
amount, positive or negative. It asks the amount's value, not its digits, so 0,
0.00 and -0.00 are all zero.
Examples
print(money.is_zero(money.zero("USD")), money.is_zero(money.of("0.01", "USD")))let a = money.of("10.25", "USD")print(money.is_zero(money.sub(a, a)), money.is_zero(money.neg(a)))output
true falsetrue falseEdge cases
- A map made by hand counts, read as
money.ofreads an amount:"0.000",0or"-0.00"is zero, since zeros at the end are not decimals the currency lacks. An amount such as"0.001"dollars is a runtime error, not zero and not money. - A product that rounds to nothing is zero:
0.01 × 0.5half to even is0.00.
print(money.is_zero({ amount: "0.000", currency: "USD" }), money.is_zero({ amount: 0, currency: "JPY" }), money.is_zero(money.of("-0.00", "USD")))print(money.is_zero(money.mul(money.of("0.01", "USD"), dec("0.5"))))output
true true truetrueErrors
A value that is not money is a runtime error, not an error value: money.is_zero(0)
stops the request, since 0 is a number, not an amount. So does a map with more
decimals than its currency has.
this one fails
print(money.is_zero(0))output
line 1: `money.is_zero` expects money ({ amount, currency }) as argument 1, got int 0this one fails
print(money.is_zero({ amount: "0.001", currency: "USD" }))output
line 1: `money.is_zero`: 0.001 has more decimals than USD has (2): round it first with money.round(m, mode)Chaining
let paid = [money.of(0, "USD"), money.of("2.50", "USD"), money.zero("USD")]print(paid | filter(money.is_zero) | len)print(money.of(5, "USD") | money.sub(money.of(5, "USD")) | money.is_zero)output
2trueSee also
money.zero, money.compare, money.sub, money.neg
Thread
Thread 0 posts
On money.is_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