Module: HumanizeNumber

Defined in:
lib/humanize-number.rb,
lib/humanize-number/version.rb

Constant Summary collapse

THOUSAND =
1000
MILLION =
1_000_000
BILLION =
1_000_000_000
VERSION =
"0.3.0"

Class Method Summary collapse

Class Method Details

.humanize(number) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/humanize-number.rb', line 8

def self.humanize(number)
  return number.to_s unless number.is_a?(Numeric)

  case 
  when number.abs < THOUSAND
    number.to_s
  when number.abs < MILLION
    "#{(number / THOUSAND.to_f).round(3 - num_of_digits(number / THOUSAND))}k"
  when number.abs < BILLION
    "#{(number / MILLION.to_f).round(3 - num_of_digits(number / MILLION))}m"
  else
    "#{(number / BILLION.to_f).round([3 - num_of_digits(number / BILLION), 0].max)}b"
  end
end