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

Link to this function

add_decimal(decimal, decimal)

View Source
@spec add_decimal(nil | Decimal.t(), nil | Decimal.t()) :: Decimal.t()

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")
@spec any?(Decimal.t() | number() | nil) :: boolean()

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
@spec as_string(term()) :: nil | String.t()

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"
@spec empty?(Decimal.t() | number() | nil) :: boolean()

Reverse of any?

@spec parse_float(any()) :: integer() | float() | nil

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
@spec parse_int(any()) :: integer() | float() | nil

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
@spec to_decimal(any()) :: Decimal.t()

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")