Skip to content

decimal

Package decimal adds exact base-10 arithmetic scalar functions, for workloads where binary floating point’s rounding is unacceptable (money, tax, billing). Values are decimal strings computed through math/big.Rat, so add / sub / mul / compare are exact; division renders to a fixed number of places.

decimal(x) -- normalize to canonical decimal text
decimal_add(a, b) -- a + b (exact)
decimal_sub(a, b) -- a - b (exact)
decimal_mul(a, b) -- a * b (exact)
decimal_div(a, b) -- a / b (to 30 places)
decimal_cmp(a, b) -- -1 / 0 / 1
decimal_neg(x) -- -x
decimal_abs(x) -- |x|
decimal_round(x, n) -- round to n fractional digits
decimal_floor(x) -- greatest integer <= x
decimal_ceil(x) -- least integer >= x
decimal_sum(x) -- exact aggregate sum

Inputs may be TEXT, INTEGER, or REAL; a REAL is read as the shortest decimal that round-trips it (so decimal_add(0.1, 0.2) is exactly 0.3, not 0.30000000000000004). For full exactness, store decimals as TEXT. Any NULL argument yields NULL.


Full API: pkg.go.dev/gosqlite.org/ext/decimal