Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/numeric_humanize.rb

Constant Summary collapse

SCALE_TO_WORD =
Hash.new do |h, i|
    " * 10^#{i * 3}"
  end.merge({ 1  => " thousand",
  2  => " million",
  3  => " billion",
  4  => " trillion"
})

Instance Method Summary collapse

Instance Method Details

#humanize(figures = 3) ⇒ Object

in my unscientific test, no one really found names beyond trillion useful.



11
12
13
14
15
16
17
# File 'lib/numeric_humanize.rb', line 11

def humanize(figures = 3)
  scale = (Math.log10(self) / 3).floor
  base = 1000 ** scale
  suffix = SCALE_TO_WORD[scale.abs]
  suffix = "#{suffix}ths" if scale < 1 && suffix
  sprintf("%.#{ figures }G", self.to_f / base) + suffix.to_s
end