Krug.NumberUtil (Krug v2.0.27) View Source
Utilitary safe module for some numeric (non Math) operations/transformations.
Link to this section Summary
Functions
Verify if a value received is a valid number. If is valid return the
value received passing by to_integer()
or toFloat
.
Otherwise return the value_if_empty_or_nil
parameter value.
Verify if a value received is a valid number and is >= min and <= max.
Return if a value cannot be converted to a number.
Return the max integer value.
Return a float value if a value received can be converted to a number. Otherwise return 0.
Applies to_float()
to a received value, then format with
decimals
decimal digits, using ,
(default) or .
.
Return an integer value if a value received can be converted to a number. Otherwise return 0.
Convert any number or string that could be converted in a number to a positive float number.
Link to this section Functions
Verify if a value received is a valid number. If is valid return the
value received passing by to_integer()
or toFloat
.
Otherwise return the value_if_empty_or_nil
parameter value.
Useful to forces a default value, to validations for example.
Examples
iex > Krug.NumberUtil.coalesce(nil,1)
1
iex > Krug.NumberUtil.coalesce("",1)
1
iex > Krug.NumberUtil.coalesce(" ",1)
1
iex > Krug.NumberUtil.coalesce("1-1",1)
1
iex > Krug.NumberUtil.coalesce("1A",1)
1
iex > Krug.NumberUtil.coalesce(0,1)
0
iex > Krug.NumberUtil.coalesce(0,1,true)
1
iex > Krug.NumberUtil.coalesce("0",1,true)
1
iex > Krug.NumberUtil.coalesce(2,1)
2
iex > Krug.NumberUtil.coalesce("2",1)
2
iex > Krug.NumberUtil.coalesce("1.2,4",1)
12.4
iex > Krug.NumberUtil.coalesce("1,2,4",1)
12.4
iex > Krug.NumberUtil.coalesce("-1,2.4",1)
-12.4
Verify if a value received is a valid number and is >= min and <= max.
If the value received not is a valid number return 0.
If the min
or max
parameter value not is a valid number return the value received.
If value < min return min.
If value > max return max.
Examples
iex > Krug.NumberUtil.coalesce_interval(nil,"10","20")
0
iex > Krug.NumberUtil.coalesce_interval("",10,"20")
0
iex > Krug.NumberUtil.coalesce_interval("1-1",10,20)
0
iex > Krug.NumberUtil.coalesce_interval("1A",10,20)
0
iex > Krug.NumberUtil.coalesce_interval("101","10",nil)
101
iex > Krug.NumberUtil.coalesce_interval("101","1A",20)
101
iex > Krug.NumberUtil.coalesce_interval(101,10,20)
20
iex > Krug.NumberUtil.coalesce_interval("5.5","10",20)
10
iex > Krug.NumberUtil.coalesce_interval("15.5",10,20)
15.5
Return if a value cannot be converted to a number.
Example
iex > Krug.NumberUtil.is_nan(10)
false
iex > Krug.NumberUtil.is_nan("10")
false
iex > Krug.NumberUtil.is_nan("-1.0")
false
iex > Krug.NumberUtil.is_nan("-1,0")
false
iex > Krug.NumberUtil.is_nan("10A")
true
iex > Krug.NumberUtil.is_nan("-1-1")
true
iex > Krug.NumberUtil.is_nan("1-1")
true
iex > Krug.NumberUtil.is_nan(".5")
true
iex > Krug.NumberUtil.is_nan("-.5")
true
iex > Krug.NumberUtil.is_nan(",5")
true
iex > Krug.NumberUtil.is_nan("-,5")
true
Return the max integer value.
Useful for validations for database int(11) columns for example.
Example
iex > Krug.NumberUtil.max_integer()
4294967295
Return a float value if a value received can be converted to a number. Otherwise return 0.
Examples
iex > Krug.NumberUtil.to_float(nil)
0.0
iex > Krug.NumberUtil.to_float("")
0.0
iex > Krug.NumberUtil.to_float("1,,2")
0.0
iex > Krug.NumberUtil.to_float("1-2")
0.0
iex > Krug.NumberUtil.to_float("-1..2")
0.0
iex > Krug.NumberUtil.to_float("1A")
0.0
iex > Krug.NumberUtil.to_float(-1.2)
-1.2
iex > Krug.NumberUtil.to_float("-1.2")
-1.2
iex > Krug.NumberUtil.to_float("-1,2")
-1.2
iex > Krug.NumberUtil.to_float("1.2")
1.2
iex > Krug.NumberUtil.to_float("1,2")
1.2
iex > Krug.NumberUtil.to_float("1.2,4")
12.4
iex > Krug.NumberUtil.to_float("1,2,4")
12.4
iex > Krug.NumberUtil.to_float("-1,2.4")
-12.4
to_float_format(number, decimals, comma_as_decimal_separator \\ true)
View SourceApplies to_float()
to a received value, then format with
decimals
decimal digits, using ,
(default) or .
.
Examples
iex > Krug.NumberUtil.to_float_format(nil,2)
0,00
iex > Krug.NumberUtil.to_float_format("1A",2)
0,00
iex > Krug.NumberUtil.to_float_format("1,2",2)
1,20
iex > Krug.NumberUtil.to_float_format("1,2",5)
1,20000
iex > Krug.NumberUtil.to_float_format(nil,2,false)
0.00
iex > Krug.NumberUtil.to_float_format("1A",2,false)
0.00
iex > Krug.NumberUtil.to_float_format("1,2",2,false)
1.20
iex > Krug.NumberUtil.to_float_format("1,2",5,false)
1.20000
Return an integer value if a value received can be converted to a number. Otherwise return 0.
Examples
iex > Krug.NumberUtil.to_integer(nil)
0
iex > Krug.NumberUtil.to_integer("")
0
iex > Krug.NumberUtil.to_integer("1,,2")
0
iex > Krug.NumberUtil.to_integer("1-2")
0
iex > Krug.NumberUtil.to_integer("-1..2")
0
iex > Krug.NumberUtil.to_integer("1A")
0
iex > Krug.NumberUtil.to_integer(-1.2)
-1
iex > Krug.NumberUtil.to_integer("-1.2")
-1
iex > Krug.NumberUtil.to_integer("-1,2")
-1
iex > Krug.NumberUtil.to_integer("1.2")
1
iex > Krug.NumberUtil.to_integer("1,2")
1
Convert any number or string that could be converted in a number to a positive float number.
If the number
received its not a number/cannot be converted to one,
then return 0.0
.
Examples
iex > Krug.NumberUtil.to_positive(-10)
10
iex > Krug.NumberUtil.to_positive("-10,5")
10.5
iex > Krug.NumberUtil.to_positive("-10.5")
10.5
iex > Krug.NumberUtil.to_positive("-10,5A")
0.0
iex > Krug.NumberUtil.to_positive("-1-0,5")
0.0
iex > Krug.NumberUtil.to_positive("1-0,5")
0.0
iex > Krug.NumberUtil.to_positive("1.0.5")
10.5
iex > Krug.NumberUtil.to_positive("1,0,5")
10.5
iex > Krug.NumberUtil.to_positive("1.0,5")
10.5
iex > Krug.NumberUtil.to_positive("1,0.5")
10.5
iex > Krug.NumberUtil.to_positive("-1,0,5")
10.5