@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.
type Decimal {
}Decimal.clamp#
Returns this value limited to low through high; low greater than high is
a programming fault.
fn clamp(low Decimal, high Decimal) DecimalDecimal.clone#
Returns an independent copy of this value.
fn clone() DecimalDecimal.compare#
Returns -1, 0 or 1 as this value is less than, equal to or greater than other;
trailing zeros do not matter.
fn compare(other Decimal) IntDecimal.max#
Returns the greater of this value and other, or this value when they compare
equal.
fn max(other Decimal) DecimalDecimal.min#
Returns the lesser of this value and other, or this value when they compare
equal.
fn min(other Decimal) DecimalDecimal.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.
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.
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.
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.
fn to_string() StringDecimal.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.
fn truncate_to_int() Int?RoundingMode Enum#
How a Decimal rounds to a multiple of the requested power of ten.
enum RoundingMode {
HalfEven
HalfAwayFromZero
TowardZero
Floor
Ceil
}RoundingMode.Ceil#
Rounds toward positive infinity.
CeilRoundingMode.Floor#
Rounds toward negative infinity.
FloorRoundingMode.HalfAwayFromZero#
Nearest value; an exact tie rounds away from zero.
HalfAwayFromZeroRoundingMode.HalfEven#
Nearest value; an exact tie chooses the even coefficient.
HalfEvenRoundingMode.TowardZero#
Discards the fractional coefficient toward zero.
TowardZeroparse_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.
fn parse_decimal(text String) Result[Decimal, Error] { ... }