README

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::coefficient

Returns the coefficient (significant digits) of the decimal.

#
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_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::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::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::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::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_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