Skip to content

decimal — exact numbers for money ​

mzs
include decimal

INFO

Available in bot scripts.

float is binary: 0.1 + 0.2 is 0.30000000000000004. A decimal keeps the digits as written.

FunctionWhat it does
decimal.of(x)a string or int → a decimal; a float is an error
decimal.plus(a, b, …)sum
decimal.minus(a, b)difference
decimal.times(a, b, …)product
decimal.div(a, b)quotient, exact only, otherwise an error
decimal.div(a, b, places, mode)quotient rounded to places digits
decimal.neg(a), decimal.abs(a)negation, absolute value
decimal.cmp(a, b)-1, 0 or 1
decimal.round(a, places, mode)round; mode is "half_up" (the default) or "half_even"
decimal.str(a), decimal.str(a, places)to a string; with places, exactly that many digits
decimal.float(a), decimal.int(a)to float (lossy), to int (drops the fraction)
decimal.sum(xs)sum of an array
decimal.split(a, ways, places)split into ways parts without losing a cent

All functions except of accept an int too.

Calculating ​

mzs
include decimal
price = decimal.of("1500.35")
vat = decimal.times(price, decimal.of("0.20"))
decimal.str(decimal.plus(price, vat), 2)  # → "1800.42"
decimal.str(decimal.times(decimal.of("1.05"), 3))  # → "3.15"
decimal.str(decimal.minus(decimal.of("1"), decimal.of("0.01")))  # → "0.99"
decimal.plus(decimal.of("0.1"), decimal.of("0.2")) == decimal.of("0.3")  # → true

A cart total:

mzs
include decimal
cart = [{price: "199.90", qty: 2}, {price: "49.99", qty: 1}]
total = decimal.sum(cart.map { decimal.times(decimal.of(it["price"]), it["qty"]) })
decimal.str(total, 2)  # → "449.79"

Dividing and rounding ​

mzs
include decimal
decimal.str(decimal.div(decimal.of(10), decimal.of(4)))  # → "2.5"
decimal.str(decimal.div(decimal.of(1), decimal.of(3), 4))  # → "0.3333"
decimal.str(decimal.round(decimal.of("2.665"), 2))  # → "2.67"
decimal.str(decimal.round(decimal.of("2.665"), 2, "half_even"))  # → "2.66"
decimal.str(decimal.round(decimal.of("1250"), -2))  # → "1300"
decimal.str(decimal.of("1.5"), 2)  # → "1.50"

1 / 3 without places is an error: there is no exact answer.

mzs
include decimal
decimal.div(decimal.of(1), decimal.of(3))
text
has no exact decimal form within 18 places

Splitting a sum ​

The remainder goes to the first parts; the parts add up to the whole.

mzs
include decimal
parts = decimal.split(decimal.of("10"), 3, 2)
parts.map { decimal.str(it, 2) }  # → ["3.34","3.33","3.33"]
decimal.str(decimal.sum(parts), 2)  # → "10.00"

Comparing and sorting ​

mzs
include decimal
decimal.cmp(decimal.of("9"), decimal.of("10"))  # → -1
decimal.of("1.50") == decimal.of("1.5")  # → true
["9.5", "10", "0.75"].map { decimal.of(it) }.sort { (a, b) -> decimal.cmp(a, b) }.map { decimal.str(it) }  # → ["0.75","9.5","10"]

< and + between decimals are an error: they are dicts. Use cmp and plus.

mzs
include decimal
decimal.of("1.5") + decimal.of("2")
text
cannot add Decimal to Decimal

What's inside ​

A decimal is a dict {units, scale}: the value is units × 10^-scale. That is how it is written to JSON and read back.

mzs
include decimal
decimal.of("1500.35")  # → {"units":150035,"scale":2}
type(decimal.of("1.5"))  # → "Decimal"
decimal.str(decimal.of({units: 150035, scale: 2}), 2)  # → "1500.35"

For JSON meant for people, write a string: {total: decimal.str(d, 2)}.json.

Errors ​

mzs
include decimal
try decimal.of("1 500,35") else (e) -> e["kind"]  # → "decimal"
try decimal.of(1500.35) else (e) -> e["kind"]  # → "type"
try decimal.div(decimal.of(1), 0) else (e) -> e["kind"]  # → "zero-division"

of reads only digits, a point and a sign: no space separators, no comma, no exponent. More than 18 digits after the point is an error.