Module: Humanize

Included in:
Numeric
Defined in:
lib/humanize.rb

Instance Method Summary collapse

Instance Method Details

#humanizeObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/humanize.rb', line 8

def humanize
  num = self.to_i
  o = ''
  if num < 0
    o += 'negative '
    num = num.abs
  end
  if num.zero?
    o += 'zero'
  else
    sets = []
    i = 0
    f = false
    while !num.zero?
      num, r = num.divmod(1000)
      sets << LOTS[i] + (!sets.empty? ? (f ? ' and' : ',') : '') if !(r.zero? || i.zero?)
      f = true if i.zero? && r < 100
      
      sets << SUB_ONE_THOUSAND[r] if !r.zero?
      i = i.succ
      
    end
    o += sets.reverse.join(' ')
  end
  
  o += ' point ' + self.to_s.split(/\./, 2).last.scan(/./).map { |n| SUB_ONE_THOUSAND[n.to_i] }.join(' ') if self.class == Float
  o
end