module UsefulUtilities::Numeric
Numeric
utilities
Constants
- BILLION
- MILLION
- THOUSAND
- ZERO
Public Instance Methods
float_or_integer(value)
click to toggle source
@param value [Numeric] @return [Numeric] value if value can not be coerced to integer
# File lib/useful_utilities/numeric.rb, line 22 def float_or_integer(value) value == value.to_i ? value.to_i : value end
positive_or_zero(value)
click to toggle source
@param value [Numeric] @return [Numeric] @example
UsefulUtilities::Numeric.positive_or_zero(1) #=> 1 UsefulUtilities::Numeric.positive_or_zero(-1) #=> 0
# File lib/useful_utilities/numeric.rb, line 16 def positive_or_zero(value) (value > ZERO) ? value : ZERO end
to_decimal(value, scale: nil)
click to toggle source
@param value [Numeric] @option scale [Integer] :scale (nil) @return [BigDecimal] value as BigDecimale rounded to scale
# File lib/useful_utilities/numeric.rb, line 29 def to_decimal(value, scale: nil) result = value.to_f.to_d scale ? result.round(scale) : result end
to_giga(value, unit)
click to toggle source
@param value [Numeric] @param unit [Symbol] @return [Numeric] value converted to giga
# File lib/useful_utilities/numeric.rb, line 48 def to_giga(value, unit) if unit == :k then value.fdiv(MILLION) elsif unit == :M then value.fdiv(THOUSAND) else unsupported_unit!(unit) end end
to_kilo(value, unit)
click to toggle source
@param value [Numeric] @param unit [Symbol] @return [Numeric] value converted to kilo
# File lib/useful_utilities/numeric.rb, line 38 def to_kilo(value, unit) if unit == :M then value * THOUSAND elsif unit == :G then value * MILLION else unsupported_unit!(unit) end end
to_number(value, unit)
click to toggle source
@param value [Numeric] @param unit [Symbol] @return [Numeric] value converted to number
# File lib/useful_utilities/numeric.rb, line 59 def to_number(value, unit) if unit == :M then value * MILLION elsif unit == :G then value * BILLION else unsupported_unit!(unit) end end
Private Instance Methods
unsupported_unit!(unit)
click to toggle source
# File lib/useful_utilities/numeric.rb, line 68 def unsupported_unit!(unit) raise ArgumentError.new("Unsupported unit - #{ unit }") end