March Docs

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

ptypeDecimalDecimal = Decimal(Int, Int)#

Functions

fnabsabs(d : Decimal) : Decimal#

Returns the absolute value.

fnaddadd(a : Decimal, b : Decimal) : Decimal#

Adds two Decimals. Result has the larger of the two scales.

fncoefficientcoefficient(d : Decimal) : Int#

Returns the unscaled Int coefficient.

fncomparecompare(a : Decimal, b : Decimal) : Int#

Compares two Decimals. Returns -1, 0, or 1.

fndivdiv(a : Decimal, b : Decimal, precision : {Int | _ >= 0}) : Decimal#

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.
fndiv_rounddiv_round(a : Decimal, b : Decimal, precision : Int) : Decimal#

Divides a by b to precision decimal places using half-up rounding:

  1. Panics if b is zero.
Equivalent to `round(div(a, b, precision + 1), precision)` but avoids
the intermediate allocation and re-scaling.
fneqeq(a : Decimal, b : Decimal) : Bool#

Returns true if two Decimals are equal.

fnfrom_intfrom_int(n : Int) : Decimal#

Creates a Decimal from a native Int (scale 0).

fnfrom_int_scaledfrom_int_scaled(n : Int, scale : Int) : Decimal#

Creates a Decimal from a native Int with a given scale.

fnfrom_stringfrom_string(s : String) : Decimal#

Creates a Decimal from a string like \

fngtgt(a : Decimal, b : Decimal) : Bool#

Returns true if a > b.

fnis_zerois_zero(d : Decimal) : Bool#

Returns true if the Decimal is zero.

fnltlt(a : Decimal, b : Decimal) : Bool#

Returns true if a < b.

fnmulmul(a : Decimal, b : Decimal) : Decimal#

Multiplies two Decimals. Result scale is the sum of the two scales.

fnroundround(d : Decimal, places : Int) : Decimal#

Rounds a Decimal to places decimal places using half-up rounding (0.5 rounds away from zero).

fnscalescale(d : Decimal) : Int#

Returns the scale (number of decimal places) of a Decimal.

fnsubsub(a : Decimal, b : Decimal) : Decimal#

Subtracts b from a. Result has the larger of the two scales.

fnto_stringto_string(d : Decimal) : String#

Converts a Decimal to its string representation (e.g. \