module Incline::Extensions::Numeric
Adds to_human
to numeric types (floats and integers).
Constants
- SHORT_SCALE
The short scale for humanizing a number.
Public Instance Methods
power_of_2?()
click to toggle source
Is this value a power of two?
# 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()
click to toggle source
Converts this value into a boolean.
A value of 0 is false, any other value is true.
# File lib/incline/extensions/numeric.rb, line 56 def to_bool self != 0 end
to_human()
click to toggle source
Formats the number using the short scale for any number over 1 million.
# 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