DarkMatter.Decimals (DarkMatter v1.1.0) 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
.
Casts an x
of type DarkMatter.numeric/0
into a Decimal.t/0
.
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
.
Compares x
of type DarkMatter.numeric/0
to y
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
.
Sums a list
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
Specs
annualize(DarkMatter.numeric(), DarkMatter.strict_numeric()) :: Decimal.t()
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
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()
Specs
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
cast_decimal!(any(), DarkMatter.Decimals.Conversion.conversion_modes()) :: Decimal.t()
Specs
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}}
Specs
cast_decimal_ok(any(), DarkMatter.Decimals.Conversion.conversion_modes()) :: {:ok, Decimal.t()} | :error
Specs
decimal_add(DarkMatter.numeric(), DarkMatter.strict_numeric()) :: Decimal.t()
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}
Specs
decimal_avg([DarkMatter.numeric()], DarkMatter.strict_numeric()) :: Decimal.t()
Specs
decimal_compare(DarkMatter.numeric(), DarkMatter.numeric()) :: DarkMatter.Decimals.Comparison.comparison()
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
decimal_div(DarkMatter.numeric(), DarkMatter.strict_numeric()) :: Decimal.t()
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}
Specs
decimal_div( DarkMatter.numeric(), DarkMatter.numeric(), DarkMatter.strict_numeric() ) :: Decimal.t()
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
decimal_mult(DarkMatter.numeric(), DarkMatter.numeric()) :: Decimal.t()
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}
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}
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
decimal_sub(DarkMatter.numeric(), DarkMatter.numeric()) :: Decimal.t()
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}
Specs
decimal_sum([DarkMatter.numeric()], DarkMatter.strict_numeric()) :: Decimal.t()
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}
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}
Specs
max_variance_percent( [DarkMatter.numeric()], DarkMatter.Decimals.Variance.minmax() ) :: Decimal.t()
Specs
rounded?(any(), DarkMatter.Decimals.Conversion.round_options()) :: boolean()
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
to_number(DarkMatter.maybe_numeric()) :: DarkMatter.maybe_number()
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
to_string(DarkMatter.Decimals.Conversion.stringable()) :: String.t() | nil
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
to_string( DarkMatter.Decimals.Conversion.stringable(), DarkMatter.Decimals.Conversion.to_string_formatter() ) :: String.t() | nil
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}
Specs
variance_percent([DarkMatter.numeric()], DarkMatter.Decimals.Variance.minmax()) :: Decimal.t()