Skip to content

Instantly share code, notes, and snippets.

@skorks
Created November 3, 2010 11:53
Show Gist options
  • Save skorks/661002 to your computer and use it in GitHub Desktop.
Save skorks/661002 to your computer and use it in GitHub Desktop.
converting integers to english
#!/usr/bin/env ruby
NTW = {
1 => "one",
2 => "two",
3 => "three",
4 => "four",
5 => "five",
6 => "six",
7 => "seven",
8 => "eight",
9 => "nine",
10 => "ten",
11 => "eleven",
12 => "twelve",
13 => "thirteen",
14 => "fourteen",
15 => "fifteen",
16 => "sixteen",
17 => "seventeen",
18 => "eighteen",
19 => "nineteen",
20 => "twenty",
30 => "thirty",
40 => "forty",
50 => "fifty",
60 => "sixty",
70 => "seventy",
80 => "eighty",
90 => "ninety",
100 => "hundred",
1000 => "thousand",
1000000 => "million",
1000000000 => "billion",
1000000000000 => "trillion"
}
class IntegerToEnglish
class << self
def convert(number)
return "zero" if number == 0
word_representation_accumulator = []
number_digits_reversed = number.to_s.reverse
digit_count = 0
number_digits_reversed.chars.each_with_index do |digit, index|
digit_as_number = Integer(digit)
skip_zero(digit_as_number) do
if digit_count == 0
word_representation_accumulator << "#{NTW[digit_as_number]}"
elsif ten_to_twenty?(digit_as_number, digit_count)
backtrack word_representation_accumulator
actual_number = Integer("#{digit}#{number_digits_reversed[index - 1]}")
multiplier = (digit_count > 1 ? 10**(digit_count - 1) : nil)
word_representation = "#{NTW[actual_number]}"
word_representation += " #{NTW[multiplier]}" if multiplier
word_representation += " and" if word_representation_accumulator.size == 1
word_representation_accumulator << word_representation
elsif twenty_to_one_hundred?(digit_count)
backtrack word_representation_accumulator
multiplier = (digit_count > 1 ? 10**(digit_count - 1) : nil)
lookup_number = digit_as_number * 10
word_representation = "#{NTW[lookup_number]}"
word_representation += " #{NTW[Integer(number_digits_reversed[index - 1])]}"
word_representation += " #{NTW[multiplier]}" if multiplier
word_representation += " and" if word_representation_accumulator.size == 1
word_representation_accumulator << word_representation
elsif digit_count == 2 || digit_count % 3 == 2
multiplier = 10**2
word_representation = "#{NTW[digit_as_number]} #{NTW[multiplier]}"
word_representation += " and" if word_representation_accumulator.size != 0
word_representation_accumulator << word_representation
else
multiplier = 10**digit_count
word_representation = "#{NTW[digit_as_number]} #{NTW[multiplier]}"
word_representation += " and" if word_representation_accumulator.size == 1
word_representation_accumulator << word_representation
end
end
digit_count += 1
end
word_representation_accumulator.reverse.join(" ")
end
def skip_zero(digit)
if digit != 0
yield
end
end
def backtrack(word_list)
word_list.pop
end
def ten_to_twenty?(digit_as_number, digit_count)
(digit_count - 1) % 3 == 0 && digit_as_number == 1
end
def twenty_to_one_hundred?(digit_count)
(digit_count - 1) % 3 == 0
end
end
end
if __FILE__ == $0
puts IntegerToEnglish.convert(0)
puts IntegerToEnglish.convert(1)
puts IntegerToEnglish.convert(3)
puts IntegerToEnglish.convert(5)
puts IntegerToEnglish.convert(11)
puts IntegerToEnglish.convert(15)
puts IntegerToEnglish.convert(25)
puts IntegerToEnglish.convert(71)
puts IntegerToEnglish.convert(40)
puts IntegerToEnglish.convert(100)
puts IntegerToEnglish.convert(101)
puts IntegerToEnglish.convert(112)
puts IntegerToEnglish.convert(123)
puts IntegerToEnglish.convert(457)
puts IntegerToEnglish.convert(999)
puts IntegerToEnglish.convert(1000)
puts IntegerToEnglish.convert(1001)
puts IntegerToEnglish.convert(1010)
puts IntegerToEnglish.convert(1011)
puts IntegerToEnglish.convert(2117)
puts IntegerToEnglish.convert(3001)
puts IntegerToEnglish.convert(13101)
puts IntegerToEnglish.convert(14001)
puts IntegerToEnglish.convert(16000)
puts IntegerToEnglish.convert(25119)
puts IntegerToEnglish.convert(65009)
puts IntegerToEnglish.convert(315119)
puts IntegerToEnglish.convert(1000001)
puts IntegerToEnglish.convert(1315119)
puts IntegerToEnglish.convert(11315119)
puts IntegerToEnglish.convert(74315119)
puts IntegerToEnglish.convert(174315119)
puts IntegerToEnglish.convert(1174315119)
puts IntegerToEnglish.convert(15174315119)
puts IntegerToEnglish.convert(35174315119)
puts IntegerToEnglish.convert(935174315119)
puts IntegerToEnglish.convert(2935174315119)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment