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 absolute_value(x: Decimal) -> Decimal

Returns the absolute value of a Decimal.

Examples

1234.56
|> dysmal.from_float
|> dysmal.absolute_value
|> dysmal.to_string
// -> "1234.56"
-1234.56
|> dysmal.from_float
|> dysmal.absolute_value
|> dysmal.to_string
// -> "1234.56"
pub fn add(x: Decimal, y: Decimal) -> Decimal

Add two Decimal numbers together.

Examples

1234.56
|> dysmal.from_float
|> dysmal.add(dysmal.from_int(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_float(333.33)
let y = dysmal.from_float(555.55)
dysmal.compare(x, y)
// -> order.Gt
let x = dysmal.from_float(555.55)
let y = dysmal.from_float(333.33)
dysmal.compare(x, y)
// -> order.Lt
let x = dysmal.from_float(333.33)
let y = dysmal.from_float(333.33)
dysmal.compare(x, y)
// -> order.Eq
pub fn compare_with_opts(
  x: Decimal,
  y: Decimal,
  opts: Opts,
) -> order.Order

Compares two Decimals with precision and rounding options, returning an order.

Examples

let x = dysmal.from_float(333.3333)
let y = dysmal.from_float(555.5555)
let opts = dysmal.Opts(1, dysmal.RoundCeiling)
dysmal.compare_with_opts(x, y, opts)
// -> order.Gt
let x = dysmal.from_float(555.5555)
let y = dysmal.from_float(333.3333)
let opts = dysmal.Opts(1, dysmal.RoundCeiling)
dysmal.compare_with_opts(x, y, opts)
// -> order.Lt
let x = dysmal.from_float(333.3999)
let y = dysmal.from_float(333.3888)
let opts = dysmal.Opts(1, dysmal.RoundCeiling)
dysmal.compare_with_opts(x, y, opts)
// -> 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_float
|> dysmal.divide(dysmal.from_int(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_int
|> dysmal.divide_with_opts(
  dysmal.from_int(3),
  dysmal.Opts(3, dysmal.RoundCeiling),
)
// -> Ok(#(333334, -3))
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) -> Result(Decimal, Nil)

Create a new Decimal from a String.

An Error is returned if the string is not a valid decimal number.

Examples

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

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

An Error is returned if the string is not a valid decimal number.

Examples

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

Check if a Decimal is zero.

Examples

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

Multiply one Decimal by another.

Examples

1234.56
|> dysmal.from_float
|> dysmal.multiply(dysmal.from_int(2))
|> dysmal.to_string
// -> "2469.12"
pub fn negate(x: Decimal) -> Decimal

Returns the negative of a Decimal.

Examples

1234.56
|> dysmal.from_float
|> dysmal.negate
|> dysmal.to_string
// -> "-1234.56"
pub fn round(x: Decimal, opts: Opts) -> Decimal

Round a Decimal with precision and rounding options.

Examples

333.33
|> dysmal.from_float
|> 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_int
|> 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_int
|> 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_float
|> dysmal.subtract(dysmal.from_int(100))
|> dysmal.to_string
// -> "1134.56"
pub fn to_string(decimal: Decimal) -> String

Convert a Decimal to a String.

Examples

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