dysmal

Types

Representation of a decimal number.

pub type Decimal

Options type for precision and rounding.

pub type Opts {
  Opts(precision: Int, rounding: RoundingAlgorithm)
}

Constructors

pub type RoundingAlgorithm {
  RoundHalfUp
  RoundHalfDown
  RoundDown
  RoundCeiling
  RoundFloor
}

Constructors

  • RoundHalfUp
  • RoundHalfDown
  • RoundDown
  • RoundCeiling
  • RoundFloor

Values

pub fn add(x: Decimal, y: Decimal) -> Decimal

Add two Decimal numbers together.

Examples

"1234.56"
|> dysmal.from_string
|> dysmal.add(dysmal.from_string("100"))
|> dysmal.to_string
// -> "1334.56"
pub fn compare(x: Decimal, y: Decimal) -> order.Order

Compares two Decimals, returning an order.

Examples

let x = dysmal.from_string("333.33")
let y = dysmal.from_string("555.55")
dysmal.compare(x, y)
// -> order.Gt
let x = dysmal.from_string("555.55")
let y = dysmal.from_string("333.33")
dysmal.compare(x, y)
// -> order.Lt
let x = dysmal.from_string("333.33")
let y = dysmal.from_string("333.33")
dysmal.compare(x, y)
// -> order.Eq
pub fn divide(x: Decimal, y: Decimal) -> Result(Decimal, Nil)

Divide one Decimal by another.

An Error is returned if the divisor is zero.

Examples

"1234.56"
|> dysmal.from_string
|> dysmal.divide(dysmal.from_string("2"))
// -> Ok(#(61728, -2))
pub fn divide_with_opts(
  x: Decimal,
  y: Decimal,
  opts: Opts,
) -> Result(Decimal, Nil)

Divide one Decimal by another with precision and rounding options.

An Error is returned if the divisor is zero.

Examples

"1000"
|> dysmal.from_string
|> dysmal.divide_with_opts(
  dysmal.from_string("3"),
  dysmal.Opts(3, dysmal.RoundCeiling),
)
// -> Ok(#(333334, -3))
pub fn fast_cmp_ffi(x: Decimal, y: Decimal) -> Int
pub fn from_float(value: Float) -> Decimal

Create a new Decimal from a Float.

Examples

dysmal.from_float(1234.56)
// -> #(123456, -2)
pub fn from_float_with_opts(value: Float, opts: Opts) -> Decimal

Create a new Decimal from a Float with precision and rounding options.

Examples

1234.56781111
|> dysmal.from_float_with_opts(dysmal.Opts(4, dysmal.RoundCeiling))
// -> #(12345679, -4)
pub fn from_int(value: Int) -> Decimal

Create a new Decimal from an Int.

Examples

dysmal.from_int(1234)
// -> #(1234, 0)
pub fn from_int_with_opts(value: Int, opts: Opts) -> Decimal

Create a new Decimal from an Int with precision and rounding options.

Examples

1234
|> dysmal.from_int_with_opts(dysmal.Opts(4, dysmal.RoundCeiling))
// -> #(1234, 0)
pub fn from_string(value: String) -> Decimal

Create a new Decimal from a String.

Examples

dysmal.from_string("1234.56")
// -> #(123456, -2)
pub fn from_string_with_opts(
  value: String,
  opts: Opts,
) -> Decimal

Create a new Decimal from a String with precision and rounding options.

Examples

"1234.56789999"
|> dysmal.from_string_with_opts(dysmal.Opts(6, dysmal.RoundFloor))
// -> #(1234567899, -6)
pub fn is_zero(x: Decimal) -> Bool

Check if a Decimal is zero.

Examples

"1234.56"
|> dysmal.from_string
|> dysmal.is_zero
// -> False
pub fn multiply(x: Decimal, y: Decimal) -> Decimal

Multiply one Decimal by another.

Examples

"1234.56"
|> dysmal.from_string
|> dysmal.multiply(dysmal.from_string("2))
|> dysmal.to_string
// -> "2469.12"
pub fn round(x: Decimal, opts: Opts) -> Decimal

Round a Decimal with precision and rounding options.

Examples

"333.33"
|> dysmal.from_string
|> dysmal.round(dysmal.Opts(1, dysmal.RoundCeiling))
// -> #(3334, -1)
pub fn square_root(x: Decimal) -> Result(Decimal, Nil)

Returns the square root of a Decimal.

An Error is returned if the number is less than zero.

Examples

"1000"
|> dysmal.from_string
|> dysmal.square_root
// -> Ok(#(316, -1))
pub fn square_root_with_opts(
  x: Decimal,
  opts: Opts,
) -> Result(Decimal, Nil)

Returns the square root of a Decimal with precision and rounding options.

An Error is returned if the number is less than zero.

Examples

 "1234"
|> dysmal.from_string
|> dysmal.square_root_with_opts(dysmal.Opts(4, dysmal.RoundCeiling))
// -> Ok(#(35128, -3))
pub fn subtract(x: Decimal, y: Decimal) -> Decimal

Subtract one Decimal from another.

Examples

"1234.56"
|> dysmal.from_string
|> dysmal.subtract(dysmal.from_string("100"))
|> dysmal.to_string
// -> "1134.56"
pub fn to_string(decimal: Decimal) -> String

Convert a Decimal to a String.

Examples

"1234.56"
|> dysmal.from_string
|> dysmal.to_string
// -> "1234.56"
Search Document