Skip to content

@skorks /integertobetterenglish.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
how integer to english would look if english was more consistent and sane when it comes to naming numbers
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.