DarkMatter.Decimals (DarkMatter v1.1.2) View Source

Decimal Utils

Link to this section Summary

Functions

Annualize a monthly amount

Casts an x of type DarkMatter.numeric/0 into a Decimal.t/0.

Adds x and y of type DarkMatter.numeric/0.

Averages a list of type DarkMatter.numeric/0.

Divides x and y of type DarkMatter.numeric/0.

Determines if x of type DarkMatter.numeric/0 is equivalent to y of type DarkMatter.numeric/0.

Multiplies x and y of type DarkMatter.numeric/0.

Gives the percentage of x relative to y of type DarkMatter.numeric/0.

Rounds an x of type DarkMatter.numeric/0 based on the opts.

Subtracts x from y of type DarkMatter.numeric/0.

Gives the decimal representation of anx of type DarkMatter.numeric/0.

Determines the max variance percent of a list of type DarkMatter.numeric/0.

Rounds whether an x of type DarkMatter.numeric/0 is already rounded according to opts

Rounds an x of type DarkMatter.numeric/0 into a integer/0.

Gives the percentage representation of anx of type DarkMatter.numeric/0.

Rounds an x of type nil or DarkMatter.numeric/0 into a String.t/0.

Determines the variance of a list of type DarkMatter.numeric/0.

Determines the variance percent of a list of type DarkMatter.numeric/0.

Link to this section Types

Specs

decimal_map() :: %{
  sign: -1 | 1,
  coef: non_neg_integer(),
  exp: non_neg_integer()
}

Link to this section Functions

Link to this function

annualize(x, default \\ 0)

View Source

Specs

Annualize a monthly amount

Examples

iex> annualize(1)
%Decimal{coef: 12, exp: 0}

iex> annualize("$145.23")
%Decimal{coef: 174276, exp: -2}

iex> annualize(nil, 1)
%Decimal{coef: 1, exp: 0}

Specs

cast_decimal(any()) :: :error | nil | Decimal.t()

Casts an x of type DarkMatter.numeric/0 into a Decimal.t/0.

Examples

iex> cast_decimal(0.11)
%Decimal{coef: 11, exp: -2}

iex> cast_decimal(%{sign: -1, coef: 11, exp: -2})
%Decimal{sign: -1, coef: 11, exp: -2}

iex> cast_decimal(%Decimal{sign: -1, coef: 11, exp: -2})
%Decimal{sign: -1, coef: 11, exp: -2}

iex> cast_decimal(1_000, :normal)
%Decimal{coef: 1_000, exp: 0}

iex> cast_decimal(1_000, :reduced)
%Decimal{coef: 1, exp: 3}

Specs

cast_decimal(any(), DarkMatter.Decimals.Conversion.conversion_modes()) ::
  :error | nil | Decimal.t()

See DarkMatter.Decimals.Conversion.cast_decimal/2.

Specs

cast_decimal!(any()) :: Decimal.t()

Casts an x of type DarkMatter.numeric/0 into a Decimal.t/0.

Raises ArgumentError if given a non-numeric.

Examples

iex> cast_decimal!(0.11)
%Decimal{coef: 11, exp: -2}

iex> cast_decimal!(nil)
** (ArgumentError) invalid argument nil

iex> cast_decimal!(1_000, :normal)
%Decimal{coef: 1_000, exp: 0}

iex> cast_decimal!(1_000, :reduced)
%Decimal{coef: 1, exp: 3}

Specs

See DarkMatter.Decimals.Conversion.cast_decimal!/2.

Specs

cast_decimal_ok(any()) :: {:ok, Decimal.t()} | :error

Casts an x of type DarkMatter.numeric/0 into a Decimal.t/0.

Returns {:ok, %Decimal{}} or :error

Examples

iex> cast_decimal_ok(0.11)
{:ok, %Decimal{coef: 11, exp: -2}}

iex> cast_decimal_ok(nil)
:error

iex> cast_decimal_ok(1_000, :normal)
{:ok, %Decimal{coef: 1_000, exp: 0}}

iex> cast_decimal_ok(1_000, :reduced)
{:ok, %Decimal{coef: 1, exp: 3}}
Link to this function

cast_decimal_ok(x, mode)

View Source

Specs

cast_decimal_ok(any(), DarkMatter.Decimals.Conversion.conversion_modes()) ::
  {:ok, Decimal.t()} | :error

See DarkMatter.Decimals.Conversion.cast_decimal_ok/2.

Specs

Adds x and y of type DarkMatter.numeric/0.

Examples

iex> decimal_add(1, 2.5)
%Decimal{coef: 35, exp: -1}

Specs

decimal_avg([DarkMatter.numeric()]) :: Decimal.t()

Averages a list of type DarkMatter.numeric/0.

Examples

iex> decimal_avg([8, 9, "10.5", 13.3, "$1.23", %Decimal{coef: 33}])
%Decimal{coef: 12505, exp: -3}

iex> decimal_avg([], 711)
%Decimal{coef: 711, exp: 0}
Link to this function

decimal_avg(list, default)

View Source

Specs

See DarkMatter.Decimals.Arithmetic.decimal_avg/2.

Specs

Compares x of type DarkMatter.numeric/0 to y of type DarkMatter.numeric/0.

Returns :eq or :gt or :lt.

Examples

iex> decimal_compare(1, 1)
:eq

iex> decimal_compare(3, 0)
:gt

iex> decimal_compare(1, 2)
:lt

Specs

Divides x and y of type DarkMatter.numeric/0.

Returns 0 or default (if given) when dividing by 0.

Examples

iex> decimal_div(30, 2.5)
%Decimal{coef: 12, exp: 0}

iex> decimal_div(0, 0)
%Decimal{coef: 0, exp: 0}

iex> decimal_div(0, 0, 989)
%Decimal{coef: 989, exp: 0}
Link to this function

decimal_div(x, y, default)

View Source

Specs

See DarkMatter.Decimals.Arithmetic.decimal_div/3.

Specs

decimal_equal?(DarkMatter.numeric(), DarkMatter.numeric()) :: boolean()

Determines if x of type DarkMatter.numeric/0 is equivalent to y of type DarkMatter.numeric/0.

Returns true or false.

Examples

iex> decimal_equal?(1, 1)
true

iex> decimal_equal?(3, 0)
false

iex> decimal_equal?(nil, 2)
** (FunctionClauseError) no function clause matching in DarkMatter.Decimals.Comparison.decimal_compare/2

Specs

Multiplies x and y of type DarkMatter.numeric/0.

Examples

iex> decimal_mult(33, 21.523)
%Decimal{coef: 710259, exp: -3}

iex> decimal_mult(0, 0)
%Decimal{coef: 0, exp: 0}

iex> decimal_mult(1, 989)
%Decimal{coef: 989, exp: 0}
Link to this function

decimal_percentage(x, y)

View Source

Specs

decimal_percentage(DarkMatter.numeric(), DarkMatter.numeric()) :: Decimal.t()

Gives the percentage of x relative to y of type DarkMatter.numeric/0.

Examples

iex> decimal_percentage(20, 100)
%Decimal{coef: 2, exp: 1}
Link to this function

decimal_round_ok(x, opts)

View Source

Specs

decimal_round_ok(any(), DarkMatter.Decimals.Conversion.round_options()) ::
  {:ok, Decimal.t()} | :error

Rounds an x of type DarkMatter.numeric/0 based on the opts.

Returns round_up * ((x + (round_up/2)) / round_up)

Examples

iex> decimal_round_ok(25.11, round_up: 50)
{:ok, %Decimal{coef: 5, exp: 1}}

iex> decimal_round_ok(50, round_up: 50)
{:ok, %Decimal{coef: 5, exp: 1}}

iex> decimal_round_ok(0, round_up: 50)
{:ok, %Decimal{coef: 0, exp: 0}}

Specs

Subtracts x from y of type DarkMatter.numeric/0.

Examples

iex> decimal_sub(1, 2.5)
%Decimal{sign: -1, coef: 15, exp: -1}

Specs

decimal_sum([DarkMatter.numeric()]) :: Decimal.t()

Sums a list of type DarkMatter.numeric/0.

Examples

iex> decimal_sum([8, 9, "10.5", 13.3, "$1.23", %Decimal{coef: 33}])
%Decimal{coef: 7503, exp: -2}

iex> decimal_sum([], 711)
%Decimal{coef: 711, exp: 0}
Link to this function

decimal_sum(list, default)

View Source

Specs

See DarkMatter.Decimals.Arithmetic.decimal_sum/2.

Specs

from_percentage(DarkMatter.numeric()) :: Decimal.t()

Gives the decimal representation of anx of type DarkMatter.numeric/0.

Examples

iex> from_percentage(25)
%Decimal{coef: 25, exp: -2}
Link to this function

max_variance_percent(list)

View Source

Specs

max_variance_percent([DarkMatter.numeric()]) :: Decimal.t()

Determines the max variance percent of a list of type DarkMatter.numeric/0.

Defaults to 100 if given an empty list.

Examples

iex> max_variance_percent([8, 9, "10.5", 13.3, "$1.23", %Decimal{coef: 33}])
%Decimal{coef: 2638944422231107556977209116, exp: -25}

iex> max_variance_percent([])
%Decimal{coef: 1, exp: 2}

iex> max_variance_percent([], {0, 100})
%Decimal{coef: 0, exp: 0}

iex> max_variance_percent([1], {0, 100})
%Decimal{coef: 1, exp: 2}
Link to this function

max_variance_percent(list, default)

View Source

Specs

See DarkMatter.Decimals.Variance.max_variance_percent/2.

Specs

Rounds whether an x of type DarkMatter.numeric/0 is already rounded according to opts

Examples

iex> rounded?(25.11, round_up: 50)
false

iex> rounded?(50, round_up: 50)
true

iex> rounded?(0, round_up: 50)
true

Specs

Rounds an x of type DarkMatter.numeric/0 into a integer/0.

Examples

iex> to_number(0.11)
0.11

iex> to_number(%Decimal{coef: 124_225, exp: -3})
124.225

iex> to_number("$0")
0

iex> to_number(nil)
nil

iex> to_number("xyz")
nil

Specs

to_percentage(DarkMatter.numeric()) :: Decimal.t()

Gives the percentage representation of anx of type DarkMatter.numeric/0.

Examples

iex> to_percentage(0.25)
%Decimal{coef: 25, exp: 0}

Specs

Rounds an x of type nil or DarkMatter.numeric/0 into a String.t/0.

Examples

iex> to_string(%Decimal{coef: 12, exp: -10})
"0.0000000012"

iex> to_string(%Decimal{coef: 124_225, exp: -3})
"124.225"

iex> to_string("$0")
"0"

iex> to_string(nil)
nil

iex> to_string("xyz")
** (Decimal.Error): number parsing syntax: "xyz"

Specs

See DarkMatter.Decimals.Conversion.to_string/2.

Specs

variance([DarkMatter.numeric()]) :: Decimal.t()

Determines the variance of a list of type DarkMatter.numeric/0.

Defaults to 0 if given an empty or single item list.

Examples

iex> variance([8, 9, "10.5", 13.3, "$1.23", %Decimal{coef: 33}])
%Decimal{coef: 11688055, exp: -5}

iex> variance([])
%Decimal{coef: 0, exp: 0}

iex> variance([1_000])
%Decimal{coef: 0, exp: 0}

Specs

variance_percent([DarkMatter.numeric()]) :: Decimal.t()

Determines the variance percent of a list of type DarkMatter.numeric/0.

Defaults to 0 if given an empty list or 100 if given a single item list.

Examples

iex> variance_percent([8, 9, "10.5", 13.3, "$1.23", %Decimal{coef: 33}])
%Decimal{coef: 2831837255702387281334649757, exp: -25}

iex> variance_percent([])
%Decimal{coef: 0, exp: 0}

iex> variance_percent([1_000])
%Decimal{coef: 1, exp: 2}

iex> variance_percent([], {0, 100})
%Decimal{coef: 0, exp: 0}

iex> variance_percent([1], {0, 100})
%Decimal{coef: 1, exp: 2}
Link to this function

variance_percent(list, default)

View Source

Specs

See DarkMatter.Decimals.Variance.variance_percent/2.