Rujira.Math (rujira_ex v0.0.1)

Copy Markdown View Source

Math utilities for Rujira financial calculations

Summary

Functions

Average of two numbers

Divide two numbers and round down to integer

Round down to integer using floor

Multiply two numbers and round down to integer

Convert number from one decimal precision to another

Safe division that returns 0 if divisor is zero

Parses any value to a Decimal. nil passes through.

Parses any value to an integer. nil passes through.

Functions

avg(a, b)

@spec avg(number() | Decimal.t(), number() | Decimal.t()) :: Decimal.t()

Average of two numbers

div_floor(a, b)

@spec div_floor(number() | Decimal.t(), number() | Decimal.t()) :: integer()

Divide two numbers and round down to integer

floor(a)

@spec floor(number() | Decimal.t()) :: integer()

Round down to integer using floor

mul_floor(a, b)

@spec mul_floor(number() | Decimal.t(), number() | Decimal.t()) :: integer()

Multiply two numbers and round down to integer

normalize(a, from \\ 0, to \\ Rujira.Amount.decimals())

@spec normalize(number() | float() | Decimal.t(), integer(), integer()) :: Decimal.t()

Convert number from one decimal precision to another

safe_div(a, b)

@spec safe_div(number() | Decimal.t(), number() | Decimal.t()) :: Decimal.t()

Safe division that returns 0 if divisor is zero

to_decimal(value)

@spec to_decimal(nil | integer() | float() | String.t() | Decimal.t()) ::
  {:ok, Decimal.t() | nil} | {:error, :invalid_decimal}

Parses any value to a Decimal. nil passes through.

Returns {:ok, Decimal.t}, {:ok, nil}, or {:error, :invalid_decimal}.

to_integer(value)

@spec to_integer(nil | integer() | String.t()) ::
  {:ok, integer() | nil} | {:error, :invalid_integer}

Parses any value to an integer. nil passes through.

Returns {:ok, integer}, {:ok, nil}, or {:error, :invalid_integer}.