Manuscript Documentation
v2026.12.0

@std/math/decimal.ms#

Import each required symbol with import Name from "@std/math/decimal.ms".

Decimal Native type#

An exact decimal value. Conversions are explicit because binary floats may round.

Manuscript
type Decimal {
}

Decimal.clamp#

Returns this value limited to low through high; low greater than high is a programming fault.

Manuscript
fn clamp(low Decimal, high Decimal) Decimal

Decimal.clone#

Returns an independent copy of this value.

Manuscript
fn clone() Decimal

Decimal.compare#

Returns -1, 0 or 1 as this value is less than, equal to or greater than other; trailing zeros do not matter.

Manuscript
fn compare(other Decimal) Int

Decimal.max#

Returns the greater of this value and other, or this value when they compare equal.

Manuscript
fn max(other Decimal) Decimal

Decimal.min#

Returns the lesser of this value and other, or this value when they compare equal.

Manuscript
fn min(other Decimal) Decimal

Decimal.round#

Rounds exactly to a multiple of 10^-places, without converting to Float. places ranges from -6144 through 6176; negative places round to tens, hundreds and so on. Invalid places return EINVAL; an unrepresentable result returns ERANGE. An omitted or null mode selects HalfEven. The result keeps no trailing zeros, so use to_fixed to format a fixed number of digits.

Manuscript
fn round(places Int, mode RoundingMode? = null) Result[Decimal, Error]

Decimal.to_fixed#

Rounds exactly and formats in plain decimal notation with exactly digits fractional digits, keeping trailing zeros. digits ranges from 0 through 6176; any other value returns EINVAL. An omitted or null mode selects HalfEven. A result that rounds to zero has no minus sign.

Manuscript
fn to_fixed(digits Int, mode RoundingMode? = null) Result[String, Error]

Decimal.to_float#

Converts to the nearest Float, which can lose precision because binary floats cannot represent most decimal fractions exactly. Returns null when the value is beyond the Float range.

Manuscript
fn to_float() Float?

Decimal.to_string#

Returns the text of this value without trailing zeros: 1.2 for 1.20. Plain digits are used for moderate magnitudes and exponent notation such as 1e+21 or 1e-7 for very large or small ones. Use to_fixed for a fixed number of fractional digits.

Manuscript
fn to_string() String

Decimal.truncate_to_int#

Discards the fractional part toward zero and returns the integer, so -2.9 gives -2. Returns null when the integer part does not fit in Int.

Manuscript
fn truncate_to_int() Int?

RoundingMode Enum#

How a Decimal rounds to a multiple of the requested power of ten.

Manuscript
enum RoundingMode {
    HalfEven
    HalfAwayFromZero
    TowardZero
    Floor
    Ceil
}

RoundingMode.Ceil#

Rounds toward positive infinity.

Manuscript
Ceil

RoundingMode.Floor#

Rounds toward negative infinity.

Manuscript
Floor

RoundingMode.HalfAwayFromZero#

Nearest value; an exact tie rounds away from zero.

Manuscript
HalfAwayFromZero

RoundingMode.HalfEven#

Nearest value; an exact tie chooses the even coefficient.

Manuscript
HalfEven

RoundingMode.TowardZero#

Discards the fractional coefficient toward zero.

Manuscript
TowardZero

parse_decimal Function#

Parses decimal text exactly: an optional sign, digits with an optional fraction (digits on both sides of the point), and an optional exponent, as in 19.99, -0.5 or 1e3 (the d literal without its suffix). Text that is empty, malformed, has more than 34 significant digits, or has an exponent out of range returns EINVAL. Nothing is rounded.

Manuscript
fn parse_decimal(text String) Result[Decimal, Error] { ... }