Skip to content

Instantly share code, notes, and snippets.

@pjlowry
Created January 29, 2013 23:50
Show Gist options
  • Save pjlowry/4669142 to your computer and use it in GitHub Desktop.
Save pjlowry/4669142 to your computer and use it in GitHub Desktop.
numbers => words
class Fixnum
def in_words
one_nineteen = {"one" => "1", "two" => "2", "three" => "3", "four" => "4", "five" => "5", "six" => "6", "seven" => "7", "eight" => "8", "nine" => "9", "ten" => "10", "eleven" => "11", "twelve" => "12", "thirteen" => "13", "fourteen" => "14", "fifteen" => "15", "sixteen" => "16", "seventeen" => "17", "eighteen" => "18", "nineteen" => "19"}
tens = {"twenty" => "2", "thirty" => "3", "fourty" => "4", "fifty" => "5", "sixty" => "6", "seventy" => "7", "eighty" => "8", "ninty" => "9"}
big_nums = [" hundred ", " thousand ", " million ", " billion ", " trillion "]
arrays = self.to_s.split(//)
arrayi = arrays.map {|e| e.to_i}
arrayi = arrayi.join
if self < 20
array1 = self.to_s #split number into a string
one_nineteen.key(array1)
elsif self < 100
array2 = self.to_s.split(//)
tens.key(array2[0]) + " " + one_nineteen.key(array2[1])
elsif self < 1000
array3 = self.to_s.split(//)
one_nineteen.key(array3[0]) + big_nums.fetch(0) + tens.key(array3[1]) + " " + one_nineteen.key(array3[2])
elsif self < 10000
array4 = self.to_s.split(//)
array4[0].to_i.in_words + big_nums.fetch(1) + arrayi[1..arrays.length].to_i.in_words
elsif self < 20000
array4b = self.to_s.split(//)
array4b[0..1].join("").to_i.in_words + big_nums.fetch(1) + arrayi[2..arrays.length].to_i.in_words
elsif self < 100000
array5 = self.to_s.split(//)
array5[0..1].join("").to_i.in_words + big_nums.fetch(1) + arrayi[2..arrays.length].to_i.in_words
elsif self < 1000000 # 1 million
array6 = self.to_s.split(//)
array6[0].to_i.in_words + big_nums.fetch(0) + arrayi[1..arrays.length].to_i.in_words
elsif self < 10000000 # 10 million
array7 = self.to_s.split(//)
array7[0].to_i.in_words + big_nums.fetch(2) + arrayi[1..arrays.length].to_i.in_words
elsif self < 20000000 # 20 million
array8 = self.to_s.split(//)
array8[0..1].join("").to_i.in_words + big_nums.fetch(2) + arrayi[2..arrays.length].to_i.in_words
elsif self < 100000000 # 100 million
array9 = self.to_s.split(//)
array9[0..1].join("").to_i.in_words + big_nums.fetch(2) + arrayi[2..arrays.length].to_i.in_words
elsif self < 1000000000 # 1 billion
array10 = self.to_s.split(//)
array10[0].to_i.in_words + big_nums.fetch(0) + arrayi[1..arrays.length].to_i.in_words
elsif self < 10000000000 # 10 billion
array11 = self.to_s.split(//)
array11[0].to_i.in_words + big_nums.fetch(3) + arrayi[1..arrays.length].to_i.in_words
elsif self < 20000000000 # 20 billion
array12 = self.to_s.split(//)
array12[0..1].join("").to_i.in_words + big_nums.fetch(3) + arrayi[2..arrays.length].to_i.in_words
elsif self < 100000000000 # 100 billion
array13 = self.to_s.split(//)
array13[0..1].join("").to_i.in_words + big_nums.fetch(3) + arrayi[2..arrays.length].to_i.in_words
elsif self < 1000000000000 # 1 trillion
array14 = self.to_s.split(//)
array14[0].to_i.in_words + big_nums.fetch(0) + arrayi[1..arrays.length].to_i.in_words
elsif self < 10000000000000 # 10 trillion
array15 = self.to_s.split(//)
array15[0].to_i.in_words + big_nums.fetch(4) + arrayi[1..arrays.length].to_i.in_words
end
end
end
puts "'#{8.in_words}' should equal 'eight'"
puts "'#{18.in_words}' should equal 'eighteen'"
puts "'#{36.in_words}' should equal 'thirty six'"
puts "'#{459.in_words}' should equal 'four hundred fifty nine'"
puts "'#{999.in_words}' should equal 'nine hundred ninety nine'"
puts "'#{4489.in_words}' should equal 'four thousand four hundred eighty nine'"
puts "'#{19999.in_words}' should equal 'nineteen thousand nine hundred ninety nine'"
puts "'#{88888.in_words}' should equal 'eighty thousand eight hundred eighty eight'"
puts "'#{123456.in_words}' should equal 'one hundred twenty three thousand four hundred fifty six'"
puts "'#{1234567.in_words}' should equal 'one million two hundred thirty four thousand five hundred sixty seven'"
puts "'#{12345678.in_words}' should equal 'twelve million three hundred forty five thousand six hundred seventy eight'"
puts "'#{987654321.in_words}' should equal 'nine hundred eighty seven million six hundred fifty four thousand three hundred twenty one'"
puts "'#{4567893123.in_words}' should equal 'four billion five hundred sixty seven million eight hundred ninety three thousand one hundred twenty three'"
puts "'#{87654321197.in_words}' should equal 'eighty seven billion six hundred fifty four million three hundred twenty one thousand one hundred ninty seven'"
puts "'#{999999999999.in_words}' should equal 'nine hundred ninty nine billion nine hundred ninty nine million nine hundred ninty nine thousand nine hundred ninty nine'"
puts "'#{2346456433118.in_words}' should equal 'two trillion three hundred forty six billion f hundred fifty six million four hundred thirty three thousand one hundred eighteen'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment