View Source WuunderUtils.Numbers (Wuunder Utils v0.9.0)
Helper functions for floats, ints and Decimal's
Summary
Functions
Adds two Decimal's together. Defaults back to 0.
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 a number to a string. When the given value is already a binary, it tries to parse it and output it accordingly.
Reverse of any?
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.
Tries to convert any value to a Decimal.
It will also convert a nil
to a 0.
Functions
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")
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.any?(-1)
true
iex> WuunderUtils.Numbers.any?(1)
true
iex> WuunderUtils.Numbers.any?(Decimal.new("1"))
true
iex> WuunderUtils.Numbers.any?(0.000001)
true
iex> WuunderUtils.Numbers.any?(0.0)
false
iex> WuunderUtils.Numbers.any?(0)
false
iex> WuunderUtils.Numbers.any?(Decimal.new("0"))
false
iex> WuunderUtils.Numbers.any?(Decimal.new("0.0"))
false
Tries to convert a number to a string. When the given value is already a binary, it tries to parse it and output it accordingly.
Examples
iex> WuunderUtils.Numbers.as_string(13.37)
"13.37"
iex> WuunderUtils.Numbers.as_string(Decimal.new("13.37"))
"13.37"
iex> WuunderUtils.Numbers.as_string(1337)
"1337"
iex> WuunderUtils.Numbers.as_string("1337")
"1337.0"
iex> WuunderUtils.Numbers.as_string("1abc300")
"1.0"
Reverse of any?
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
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
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")