Decimal
Decimal module: fixed-point decimal arithmetic for financial math.
Representation: Decimal(coefficient, scale) where coefficient : Int — the unscaled integer value scale : Int — number of decimal places (>= 0)
Examples: 1.23 = Decimal(123, 2) -0.01 = Decimal(-1, 2) 100 = Decimal(100, 0)
All arithmetic normalizes to the higher scale of the two operands, avoiding loss of precision. Round uses half-up (0.5 rounds away from zero).
Interface implementations: impl Eq(Decimal) — eq/2 impl Ord(Decimal) — compare/2 impl Show(Decimal) — show/1
Types
Functions
Adds two Decimals. Result has the larger of the two scales.
Divides a by b to precision decimal places using truncation toward zero. Panics if b is zero.
For half-up rounding (e.g. money arithmetic where 0.5 should round
away from zero), use `div_round` instead.Divides a by b to precision decimal places using half-up rounding:
- Panics if b is zero.
Equivalent to `round(div(a, b, precision + 1), precision)` but avoids
the intermediate allocation and re-scaling.Creates a Decimal from a native Int with a given scale.
Multiplies two Decimals. Result scale is the sum of the two scales.
Rounds a Decimal to places decimal places using half-up rounding (0.5 rounds away from zero).
Subtracts b from a. Result has the larger of the two scales.