WuunderUtils.Numbers (Wuunder Utils v0.1.1)
Helper functions for floats, ints and Decimal's
Summary
Functions
Adds two Decimal's together. Defaults back to 0.
Reverse of present?
Parses a String.t() to a float(). Just passes float() but does convert integer() to a float().
Parses a string to an int. Returns nil
if that didn't work out.
Tests if number is "present". Meaning: the number must not be 0, 0.0. Positive or negative is ok. Also a tiny fraction just above 0 is allowed.
Tries to convert any value to a Decimal.
It will also convert a nil
to a 0.
Functions
add_decimal(decimal, decimal)
Adds two Decimal's together. Defaults back to 0.
Examples
iex> WuunderUtils.Numbers.add_decimal(nil, nil)
Decimal.new("0")
iex> WuunderUtils.Numbers.add_decimal(nil, Decimal.new("15.5"))
Decimal.new("15.5")
iex> WuunderUtils.Numbers.add_decimal(Decimal.new("6.5"), Decimal.new("15.5"))
Decimal.new("22.0")
iex> WuunderUtils.Numbers.add_decimal(Decimal.new("15.5"), nil)
Decimal.new("15.5")
empty?(value)
Reverse of present?
parse_float(value)
Parses a String.t() to a float(). Just passes float() but does convert integer() to a float().
When parsing fails, this function returns a nil
.
Examples
iex> WuunderUtils.Numbers.parse_float("10005")
10005.0
iex> WuunderUtils.Numbers.parse_float(10005)
10005
iex> WuunderUtils.Numbers.parse_float(10.5)
10.5
iex> WuunderUtils.Numbers.parse_float("10.50")
10.5
iex> WuunderUtils.Numbers.parse_float("TEST10.50")
nil
iex> WuunderUtils.Numbers.parse_float("10TEST2")
10.0
parse_int(value)
Parses a string to an int. Returns nil
if that didn't work out.
Examples
iex> WuunderUtils.Numbers.parse_int("10005")
10005
iex> WuunderUtils.Numbers.parse_int(10005)
10005
iex> WuunderUtils.Numbers.parse_int(10.5)
10.5
iex> WuunderUtils.Numbers.parse_int("10.50")
10
iex> WuunderUtils.Numbers.parse_int("TEST10.50")
nil
iex> WuunderUtils.Numbers.parse_int("10TEST2")
10
present?(decimal)
Tests if number is "present". Meaning: the number must not be 0, 0.0. Positive or negative is ok. Also a tiny fraction just above 0 is allowed.
Examples
iex> WuunderUtils.Numbers.present?(-1)
true
iex> WuunderUtils.Numbers.present?(1)
true
iex> WuunderUtils.Numbers.present?(Decimal.new("1"))
true
iex> WuunderUtils.Numbers.present?(0.000001)
true
iex> WuunderUtils.Numbers.present?(0.0)
false
iex> WuunderUtils.Numbers.present?(0)
false
iex> WuunderUtils.Numbers.present?(Decimal.new("0"))
false
iex> WuunderUtils.Numbers.present?(Decimal.new("0.0"))
false
to_decimal(value)
Tries to convert any value to a Decimal.
It will also convert a nil
to a 0.
Examples
iex> WuunderUtils.Numbers.to_decimal("15")
Decimal.new("15")
iex> WuunderUtils.Numbers.to_decimal("15")
Decimal.new("15")