std.decimal
exact decimals (money); coef / 10^scale, i64 coef, max 18 places
DecimalError
Section titled “DecimalError”pub DecimalError :: errors { invalid, overflow, division_by_zero,}invalid: text that is no decimal numberoverflow: coef past i64, or more than 18 places
Rounding
Section titled “Rounding”pub Rounding :: enum { half_even, half_up, half_down, down, up, floor, ceiling,}half_even: ties to even (banker’s): 2.5 -> 2, 3.5 -> 4half_up: ties away from zero: 2.5 -> 3, -2.5 -> -3half_down: ties toward zero: 2.5 -> 2down: toward zero: 2.9 -> 2, -2.9 -> -2up: away from zero: 2.1 -> 3, -2.1 -> -3floor: toward -inf: -2.1 -> -3ceiling: toward +inf: 2.1 -> 3
MAX_SCALE
Section titled “MAX_SCALE”pub MAX_SCALE :: 18u32Decimal
Section titled “Decimal”pub Decimal :: struct { coef: i64, scale: u32,}Decimal.format
Section titled “Decimal.format”pub format :: fn(self: Decimal, f: *Formatter)for “{d}” in format strings; keeps all places
from_parts
Section titled “from_parts”pub from_parts :: fn(coef: i64, scale: u32) -> Decimal ! DecimalErrorcoef / 10^scale; .overflow if scale > 18
from_int
Section titled “from_int”pub from_int :: fn(n: i64) -> Decimalpub zero :: fn() -> Decimalpub add :: fn(a: Decimal, b: Decimal) -> Decimal ! DecimalErrorexact, at the larger scale
pub sub :: fn(a: Decimal, b: Decimal) -> Decimal ! DecimalErrorexact, at the larger scale
pub mul :: fn(a: Decimal, b: Decimal) -> Decimal ! DecimalErrorscale = a.scale + b.scale; past 18 or too big: drops places (half_even), then .overflow
pub div :: fn(a: Decimal, b: Decimal, scale: u32, mode: Rounding) -> Decimal ! DecimalErrora / b at scale places, rounded by mode
pub round :: fn(d: Decimal, scale: u32, mode: Rounding) -> Decimal ! DecimalErrormore places is exact; fewer rounds by mode
pub neg :: fn(d: Decimal) -> Decimal ! DecimalErrorpub abs :: fn(d: Decimal) -> Decimal ! DecimalErrornormalize
Section titled “normalize”pub normalize :: fn(d: Decimal) -> Decimalstrips trailing zeros: 2.500 -> 2.5, 3.00 -> 3
pub cmp :: fn(a: Decimal, b: Decimal) -> i32-1, 0, 1; ignores scale (2.50 == 2.5)
pub eq :: fn(a: Decimal, b: Decimal) -> boolpub less :: fn(a: Decimal, b: Decimal) -> boolis_zero
Section titled “is_zero”pub is_zero :: fn(d: Decimal) -> boolis_negative
Section titled “is_negative”pub is_negative :: fn(d: Decimal) -> boolpub parse :: fn(text: []const u8) -> Decimal ! DecimalErrorsign, digits, one point, optional exponent; scale = places written; .invalid, .overflow
to_text
Section titled “to_text”pub to_text :: fn(d: Decimal, out: []u8) -> []const u8keeps all places (19.90 stays 19.90); slice of out; 48 bytes always enough
to_f64
Section titled “to_f64”pub to_f64 :: fn(d: Decimal) -> f64nearest f64; exact to ~15 significant digits
from_f64
Section titled “from_f64”pub from_f64 :: fn(x: f64, scale: u32) -> Decimal ! DecimalErrorrounds half-even to scale places; floats are binary, not for exact work
pub sum :: fn(xs: []const Decimal) -> Decimal ! DecimalErrorexact, at the largest scale