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

fnfrom_stringfrom_string(s : String) : Decimal#

Creates a Decimal from a string like \

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

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

fnfrom_intfrom_int(n : Int) : Decimal#

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

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

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

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

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

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

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

fndivdiv(a : Decimal, b : Decimal, precision : Int) : Decimal#

Divides a by b to precision decimal places using truncation. Panics if b is zero.

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

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

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

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

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

Returns true if two Decimals are equal.

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

Returns true if a < b.

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

Returns true if a > b.

fnto_stringto_string(d : Decimal) : String#

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

fnscalescale(d : Decimal) : Int#

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

fncoefficientcoefficient(d : Decimal) : Int#

Returns the unscaled Int coefficient.

fnis_zerois_zero(d : Decimal) : Bool#

Returns true if the Decimal is zero.

fnabsabs(d : Decimal) : Decimal#

Returns the absolute value.