Alpha. Vary is under active development and not ready for production use. Syntax, APIs, performance, and behaviour may change between releases.

Decimal

The Decimal type provides exact decimal arithmetic backed by java.math.BigDecimal. Use it instead of Float when rounding errors are unacceptable (financial calculations, invoices, rates).

from decimal import parse, from_int, round_half_up

let price = parse("19.99").unwrap()
let qty = from_int(3)
let total = price * qty                      # 59.97 (exact)
let tax = total.divide(from_int(100), 2, round_half_up)

Constructors

FunctionReturnsDescription
parse(s)Result[Decimal, DecimalError]Parse a string into a Decimal
from_int(n)DecimalCreate a Decimal from an integer
from_float(n)DecimalCreate from float (prefer parse for exact values)

Operators

Decimal supports +, -, *, and comparison operators (==, !=, <, >, <=, >=).

Division requires explicit scale and rounding mode:

let result = a.divide(b, scale, mode)
let rounded = d.quantize(scale, mode)

Rounding modes

ConstantDescription
round_half_upRounds 0.5 away from zero; for invoices
round_half_evenBanker's rounding; minimizes bias
round_downTruncate toward zero; never exceeds
round_upRound away from zero; never undershoots