Module: UsefulUtilities::Numeric
Overview
Numeric utilities
Constant Summary collapse
- ZERO =
0
- THOUSAND =
1_000
- MILLION =
1_000_000
- BILLION =
1_000_000_000
Instance Method Summary collapse
-
#float_or_integer(value) ⇒ Numeric
Value if value can not be coerced to integer.
- #positive_or_zero(value) ⇒ Numeric
-
#to_decimal(value, scale: nil) ⇒ BigDecimal
Value as BigDecimale rounded to scale.
-
#to_giga(value, unit) ⇒ Numeric
Value converted to giga.
-
#to_kilo(value, unit) ⇒ Numeric
Value converted to kilo.
-
#to_number(value, unit) ⇒ Numeric
Value converted to number.
Instance Method Details
#float_or_integer(value) ⇒ Numeric
Returns value if value can not be coerced to integer.
22 23 24 |
# 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) ⇒ Numeric
16 17 18 |
# File 'lib/useful_utilities/numeric.rb', line 16 def positive_or_zero(value) (value > ZERO) ? value : ZERO end |
#to_decimal(value, scale: nil) ⇒ BigDecimal
Returns value as BigDecimale rounded to scale.
29 30 31 32 33 |
# 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) ⇒ Numeric
Returns value converted to giga.
48 49 50 51 52 53 |
# 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 |