Module: Incline::Extensions::Numeric
- Defined in:
- lib/incline/extensions/numeric.rb
Overview
Adds to_human to numeric types (floats and integers).
Constant Summary collapse
- SHORT_SCALE =
The short scale for humanizing a number.
[ [ Integer('1'.ljust(40,'0')), 'duodecillion' ], [ Integer('1'.ljust(37,'0')), 'undecillion' ], [ Integer('1'.ljust(34,'0')), 'decillion' ], [ Integer('1'.ljust(31,'0')), 'nonillion' ], [ Integer('1'.ljust(28,'0')), 'octilillion' ], [ Integer('1'.ljust(25,'0')), 'septillion' ], [ Integer('1'.ljust(22,'0')), 'sextillion' ], [ Integer('1'.ljust(19,'0')), 'quintillion' ], [ Integer('1'.ljust(16,'0')), 'quadrillion' ], [ Integer('1'.ljust(13,'0')), 'trillion' ], [ Integer('1'.ljust(10,'0')), 'billion' ], [ Integer('1'.ljust(7,'0')), 'million' ], [ Integer('1'.ljust(4,'0')), 'thousand' ], ]
Instance Method Summary collapse
-
#power_of_2? ⇒ Boolean
Is this value a power of two?.
-
#to_bool ⇒ Object
Converts this value into a boolean.
-
#to_human ⇒ Object
Formats the number using the short scale for any number over 1 million.
Instance Method Details
#power_of_2? ⇒ Boolean
Is this value a power of two?
62 63 64 |
# File 'lib/incline/extensions/numeric.rb', line 62 def power_of_2? (self.to_i == self) && (self != 0) && ((self & (self - 1)) == 0) end |
#to_bool ⇒ Object
Converts this value into a boolean.
A value of 0 is false, any other value is true.
56 57 58 |
# File 'lib/incline/extensions/numeric.rb', line 56 def to_bool self != 0 end |
#to_human ⇒ Object
Formats the number using the short scale for any number over 1 million.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/incline/extensions/numeric.rb', line 29 def to_human Incline::Extensions::Numeric::SHORT_SCALE.each do |(num,label)| if self >= num # Add 0.0001 to the value before rounding it off. # This way we're telling the system that we want it to round up instead of round to even. s = ('%.2f' % ((self.to_f / num) + 0.0001)).gsub(/\.?0+\z/,'') return "#{s} #{label}" end end if self.is_a?(::Rational) if self.denominator == 1 return self.numerator.to_s end return self.to_s elsif self.is_a?(::Integer) return self.to_s end # Again we want to add the 0.0001 to the value before rounding. ('%.2f' % (self.to_f + 0.0001)).gsub(/\.?0+\z/,'') end |