WuunderUtils.Numbers (Wuunder Utils v0.2.5)

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

Link to this function

add_decimal(decimal, decimal)

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

Reverse of present?

Link to this function

parse_float(value)

@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
Link to this function

parse_int(value)

@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
Link to this function

present?(decimal)

@spec present?(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.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
Link to this function

to_decimal(value)

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