public
Created

how integer to english would look if english was more consistent and sane when it comes to naming numbers

  • Download Gist
integertobetterenglish.rb
Ruby
1 2 3 4 5 6 7 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
#!/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",
100 => "hundred",
1000 => "thousand",
1000000 => "million",
1000000000 => "billion",
1000000000000 => "trillion"
}
 
class IntegerToBetterEnglish
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
multiplier = 10**digit_count
word_representation = "#{NTW[digit_as_number]}"
word_representation += " #{NTW[multiplier]}" if multiplier > 1 && NTW[multiplier]
word_representation_accumulator << word_representation
end
digit_count += 1
end
word_representation_accumulator.reverse.join(" ")
end
 
def skip_zero(digit)
if digit != 0
yield
end
end
end
end
 
if __FILE__ == $0
puts IntegerToBetterEnglish.convert(0)
puts IntegerToBetterEnglish.convert(1)
puts IntegerToBetterEnglish.convert(3)
puts IntegerToBetterEnglish.convert(5)
puts IntegerToBetterEnglish.convert(11)
puts IntegerToBetterEnglish.convert(15)
puts IntegerToBetterEnglish.convert(25)
puts IntegerToBetterEnglish.convert(71)
puts IntegerToBetterEnglish.convert(40)
puts IntegerToBetterEnglish.convert(100)
puts IntegerToBetterEnglish.convert(101)
puts IntegerToBetterEnglish.convert(112)
puts IntegerToBetterEnglish.convert(123)
puts IntegerToBetterEnglish.convert(457)
puts IntegerToBetterEnglish.convert(999)
puts IntegerToBetterEnglish.convert(1000)
puts IntegerToBetterEnglish.convert(1001)
puts IntegerToBetterEnglish.convert(1010)
puts IntegerToBetterEnglish.convert(1011)
puts IntegerToBetterEnglish.convert(2117)
puts IntegerToBetterEnglish.convert(3001)
puts IntegerToBetterEnglish.convert(13101)
puts IntegerToBetterEnglish.convert(14001)
puts IntegerToBetterEnglish.convert(16000)
puts IntegerToBetterEnglish.convert(25119)
puts IntegerToBetterEnglish.convert(65009)
puts IntegerToBetterEnglish.convert(315119)
puts IntegerToBetterEnglish.convert(1000001)
puts IntegerToBetterEnglish.convert(1315119)
puts IntegerToBetterEnglish.convert(11315119)
puts IntegerToBetterEnglish.convert(74315119)
puts IntegerToBetterEnglish.convert(174315119)
puts IntegerToBetterEnglish.convert(1174315119)
puts IntegerToBetterEnglish.convert(15174315119)
puts IntegerToBetterEnglish.convert(35174315119)
puts IntegerToBetterEnglish.convert(935174315119)
puts IntegerToBetterEnglish.convert(2935174315119)
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.