moonbitlang/x/decimal does not have a README file

    Decimal

    pub struct Decimal {
    coefficient :
    BigInt

    scale : Int
    } derive(
    Debug
    )

    Arbitrary precision decimal type for exact decimal arithmetic.

    This type is designed for financial calculations and other applications that require exact decimal representation without floating-point errors.

    The decimal is represented as a coefficient (BigInt) and a scale (Int):
    • coefficient: The significant digits as a BigInt
    • scale: The number of decimal places (0 <= scale <= max_scale)

    For example: 123.45 is represented as coefficient=12345, scale=2

    Invariants:
    • 0 <= scale <= max_scale (default 28)
    • coefficient and scale are normalized (no trailing zeros in coefficient)
    • Zero is represented as coefficient=0, scale=0

    Example:
    test {
    let price = match @decimal.Decimal::from_string("19.99") {
    Some(p) => p
    None => fail("Invalid price")
    }
    let tax = match @decimal.Decimal::from_string("0.08") {
    Some(t) => t
    None => fail("Invalid tax rate")
    }
    let total = price + tax
    inspect(total.to_string(), content="20.07")
    }

    impl Add for Decimal
    impl Compare for Decimal
    impl Default for Decimal
    impl Div for Decimal
    impl Eq for Decimal
    impl Hash for Decimal
    impl Mul for Decimal
    impl Neg for Decimal
    impl Show for Decimal
    impl Sub for Decimal
    impl ToJson for Decimal
    impl FromJson for Decimal

    Decimal::abs

    fn Decimal::abs(self : Decimal) -> Decimal

    Returns the absolute value of the decimal.

    Decimal::add

    #deprecated("`Decimal::add` is deprecated, use `Add::add` instead.")
    fn Decimal::add(self : Decimal, other : Decimal) -> Decimal

    Decimal::arbitrary

    #deprecated("`Decimal::arbitrary` is deprecated, use `@moonbitlang/core/quickcheck.Arbitrary::arbitrary` instead.")
    fn Decimal::arbitrary(size : Int, rs :
    RandomState
    ) -> Decimal

    Decimal::coefficient

    Returns the coefficient (significant digits) of the decimal.

    Decimal::compare

    #deprecated("`Decimal::compare` is deprecated, use `Compare::compare` instead.")
    fn Decimal::compare(self : Decimal, other : Decimal) -> Int

    Decimal::default

    #deprecated("`Decimal::default` is deprecated, use `Default::default` instead.")
    fn Decimal::default() -> Decimal

    Decimal::div

    #deprecated("`Decimal::div` is deprecated, use `Div::div` instead.")
    fn Decimal::div(self : Decimal, other : Decimal) -> Decimal

    Decimal::equal

    #deprecated("`Decimal::equal` is deprecated, use `Eq::equal` instead.")
    fn Decimal::equal(self : Decimal, other : Decimal) -> Bool

    Decimal::from_bigint

    Creates a decimal from a BigInt.

    Parameters:
    • n : The BigInt value

    Returns a decimal with scale 0.

    Example:
    test {
    let d = @decimal.Decimal::from_bigint(12345678901234567890N)
    inspect(d.to_string(), content="12345678901234567890")
    }

    Decimal::from_double

    fn Decimal::from_double(d : Double, precision : Int) -> Decimal?

    Creates a decimal from a double with specified precision.

    Parameters:
    • d : The double value
    • precision : Number of decimal places

    Returns Some(decimal) if conversion succeeds, None otherwise.

    Example:
    test {
    let d = match @decimal.Decimal::from_double(3.14159, 5) {
    Some(d) => d
    None => fail("Invalid double conversion")
    }
    inspect(d.to_string(), content="3.14159")
    }

    Decimal::from_int

    fn Decimal::from_int(n : Int) -> Decimal

    Creates a decimal from an integer.

    Parameters:
    • n : The integer value

    Returns a decimal with scale 0.

    Example:
    test {
    let d = @decimal.Decimal::from_int(42)
    inspect(d.to_string(), content="42")
    }

    Decimal::from_json

    #deprecated("`Decimal::from_json` is deprecated, use `@moonbitlang/core/json.FromJson::from_json` instead.")
    fn Decimal::from_json(json : Json, path :
    JsonPath
    ) -> Decimal raise
    JsonDecodeError

    Decimal::from_string

    fn Decimal::from_string(s : String) -> Decimal?

    Creates a decimal from a string representation.

    Parameters:
    • s : String representation of the decimal (e.g., "123.45", "-0.001")

    Returns Some(decimal) if parsing succeeds, None otherwise.

    Example:
    test {
    let d = match @decimal.Decimal::from_string("123.45") {
    Some(d) => d
    None => fail("Invalid decimal string")
    }
    inspect(d.to_string(), content="123.45")
    }

    Decimal::hash

    #deprecated("`Decimal::hash` is deprecated, use `Hash::hash` instead.")
    fn Decimal::hash(self : Decimal) -> Int

    Decimal::hash_combine

    #deprecated("`Decimal::hash_combine` is deprecated, use `Hash::hash_combine` instead.")
    fn Decimal::hash_combine(self : Decimal, hasher : Hasher) -> Unit

    Decimal::is_negative

    fn Decimal::is_negative(self : Decimal) -> Bool

    Checks if the decimal is negative.

    Decimal::is_positive

    fn Decimal::is_positive(self : Decimal) -> Bool

    Checks if the decimal is positive.

    Decimal::is_zero

    fn Decimal::is_zero(self : Decimal) -> Bool

    Checks if the decimal is zero.

    Decimal::mul

    #deprecated("`Decimal::mul` is deprecated, use `Mul::mul` instead.")
    fn Decimal::mul(self : Decimal, other : Decimal) -> Decimal

    Decimal::neg

    #deprecated("`Decimal::neg` is deprecated, use `Neg::neg` instead.")
    fn Decimal::neg(self : Decimal) -> Decimal

    Decimal::new

    fn Decimal::new(coefficient :
    BigInt
    , scale : Int) -> Decimal?

    Creates a decimal from a coefficient and scale.

    Parameters:
    • coefficient : The significant digits as a BigInt
    • scale : The number of decimal places

    Returns Some(decimal) if the scale is valid, None otherwise.

    Example:
    test {
    let d = match @decimal.Decimal::new(12345N, 2) {
    Some(d) => d
    None => fail("Invalid decimal")
    } // 123.45
    inspect(d.to_string(), content="123.45")
    }

    Decimal::not_equal

    #deprecated("`Decimal::not_equal` is deprecated, use `Eq::not_equal` instead.")
    fn Decimal::not_equal(x : Decimal, y : Decimal) -> Bool

    Decimal::op_ge

    #deprecated("`Decimal::op_ge` is deprecated, use `Compare::op_ge` instead.")
    fn Decimal::op_ge(x : Decimal, y : Decimal) -> Bool

    Decimal::op_gt

    #deprecated("`Decimal::op_gt` is deprecated, use `Compare::op_gt` instead.")
    fn Decimal::op_gt(x : Decimal, y : Decimal) -> Bool

    Decimal::op_le

    #deprecated("`Decimal::op_le` is deprecated, use `Compare::op_le` instead.")
    fn Decimal::op_le(x : Decimal, y : Decimal) -> Bool

    Decimal::op_lt

    #deprecated("`Decimal::op_lt` is deprecated, use `Compare::op_lt` instead.")
    fn Decimal::op_lt(x : Decimal, y : Decimal) -> Bool

    Decimal::output

    #deprecated("`Decimal::output` is deprecated, use `Show::output` instead.")
    fn Decimal::output(self : Decimal, logger : &Logger) -> Unit

    Decimal::round

    fn Decimal::round(self : Decimal, places : Int) -> Decimal?

    Rounds the decimal to the specified number of decimal places.

    Parameters:
    • self : The decimal to round
    • places : Number of decimal places (0 <= places <= max_scale)

    Returns Some(decimal) if places is valid, None otherwise.

    Example:
    test {
    let d = match @decimal.Decimal::from_string("3.14159") {
    Some(d) => d
    None => fail("Invalid decimal string")
    }
    let rounded = match d.round(2) {
    Some(r) => r
    None => fail("Invalid rounding")
    }
    inspect(rounded.to_string(), content="3.14")
    }

    Decimal::scale

    fn Decimal::scale(self : Decimal) -> Int

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

    Decimal::scale_to

    fn Decimal::scale_to(self : Decimal, new_scale : Int) -> Decimal?

    Scales the decimal to have the specified number of decimal places.

    Parameters:
    • self : The decimal to scale
    • new_scale : New number of decimal places (0 <= new_scale <= max_scale)

    Returns Some(decimal) if new_scale is valid, None otherwise.

    Example:
    test {
    let d = match @decimal.Decimal::from_string("123.45") {
    Some(d) => d
    None => fail("Invalid decimal string")
    }
    let scaled = match d.scale_to(4) {
    Some(s) => s
    None => fail("Invalid scaling")
    }
    inspect(scaled.to_string(), content="123.4500")
    }

    Decimal::signum

    fn Decimal::signum(self : Decimal) -> Int

    Returns the sign of the decimal: -1, 0, or 1.

    Decimal::sub

    #deprecated("`Decimal::sub` is deprecated, use `Sub::sub` instead.")
    fn Decimal::sub(self : Decimal, other : Decimal) -> Decimal

    Decimal::to_bigint

    Converts the decimal to a BigInt by truncating towards zero.

    Example:
    test {
    let d = match @decimal.Decimal::from_string("123.99") {
    Some(d) => d
    None => fail("Invalid decimal string")
    }
    inspect(d.to_bigint(), content="123")
    }

    Decimal::to_double

    fn Decimal::to_double(self : Decimal) -> Double

    Converts the decimal to a double (with potential precision loss).

    Warning: This conversion may lose precision for large numbers or high precision decimals.

    Example:
    test {
    let d = match @decimal.Decimal::from_string("3.14159") {
    Some(d) => d
    None => fail("Invalid decimal string")
    }
    inspect(d.to_double(), content="3.14159")
    }

    Decimal::to_int

    fn Decimal::to_int(self : Decimal) -> Int?

    Converts the decimal to an integer by truncating towards zero.

    Returns Some(int) if the decimal fits in an Int, None otherwise.

    Example:
    test {
    let d = match @decimal.Decimal::from_string("123.99") {
    Some(d) => d
    None => fail("Invalid decimal string")
    }
    debug_inspect(d.to_int(), content="Some(123)")
    }

    Decimal::to_json

    #deprecated("`Decimal::to_json` is deprecated, use `ToJson::to_json` instead.")
    fn Decimal::to_json(self : Decimal) -> Json

    Decimal::to_repr

    #deprecated("`Decimal::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
    fn Decimal::to_repr(Decimal) ->
    Repr

    Decimal::to_string

    fn Decimal::to_string(self : Decimal) -> String

    Converts the decimal to a string representation.

    Returns a string in the format "integer.fractional" or "integer".

    Example:
    test {
    let d = match @decimal.Decimal::from_string("123.45") {
    Some(d) => d
    None => fail("Invalid decimal string")
    }
    inspect(d.to_string(), content="123.45")
    }

    Decimal::truncate

    fn Decimal::truncate(self : Decimal, places : Int) -> Decimal?

    Truncates the decimal to the specified number of decimal places.

    Parameters:
    • self : The decimal to truncate
    • places : Number of decimal places (0 <= places <= max_scale)

    Returns Some(decimal) if places is valid, None otherwise.

    Example:
    test {
    let d = match @decimal.Decimal::from_string("3.14159") {
    Some(d) => d
    None => fail("Invalid decimal string")
    }
    let truncated = match d.truncate(2) {
    Some(t) => t
    None => fail("Invalid truncation")
    }
    inspect(truncated.to_string(), content="3.14")
    }

    max_scale

    let max_scale : Int

    Maximum number of decimal places (scale) allowed.

    neg_one

    fn neg_one() -> Decimal

    Returns the negative one decimal value.

    one

    fn one() -> Decimal

    Returns the one decimal value.

    zero

    fn zero() -> Decimal

    Returns the zero decimal value.

    Source Files

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io