Class: NumberWordsEn::English

Inherits:
NumberWords::Base
  • Object
show all
Defined in:
lib/number_words_en.rb

Constant Summary collapse

THOUSANDS =
["", "thousand", "million", "billion"]

Instance Method Summary collapse

Instance Method Details

#hundreds_to_words(int) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/number_words_en.rb', line 19

def hundreds_to_words int
  h = int / 100
  t = int % 100
  if t == 0
    case h;
    when 0; "zero";
    when 1; "one hundred";
    else "#{tens_to_words h} hundred"
    end
  else
    h == 0 ? "#{tens_to_words t}" : "#{tens_to_words h} hundred and #{tens_to_words t}"
  end
end

#int_to_words(int, options = { }) ⇒ Object



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

def int_to_words int, options={ }
  case int
  when 0; "zero"
  else
    join_thousands THOUSANDS, split_by_thousands(int)
  end
end

#join_thousands(names, amounts) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/number_words_en.rb', line 42

def join_thousands names, amounts
  working = amounts.zip(names)
  working.delete_if { |amount, name| amount == 0 }

  working = working.map { |amount, name|
    [hundreds_to_words(amount), name]
  }

  if amounts[0] > 0 && amounts[0] < 100 && amounts.size > 1
    huns = working.shift
    working.unshift "and"
    working.unshift huns
  end

  working.reverse.flatten.join(' ')
end

#tens_to_words(i) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/number_words_en.rb', line 33

def tens_to_words i
  if i < 20
    WORDS["numbers"]["initial"][i]
  else
    tens = WORDS["numbers"]["tens"][i / 10]
    "#{tens} #{tens_to_words(i % 10)}"
  end
end