Created
January 23, 2013 05:27
-
-
Save mfpiccolo/4602301 to your computer and use it in GitHub Desktop.
Use this method on a positive integer less than one quadrillion and it will return it in english.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Fixnum | |
def in_words | |
first_twenty = { "0" => "Zero", "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" } | |
tens = { "2" => "Twenty", "3" => "Thirty", "4" => "Fourty", "5" => "Fifty", "6" => "Sixty", "7" => "Seventy", "8" => "Eighty", | |
"9" => "Ninety" } | |
big_number = { 3 => "-Hundred and ", 4 => "-Thousand", 5 => "-Thousand", 6 => "-Thousand", 7 => "-Million", 8 => "-Million", 9 => "-Million", 10 => "-Billion", 11 => "-Billion", 12 => "-Billion", | |
13 => "-Trillion", 14 => "-Trillion", 15 => "-Trillion" } | |
number_string = self.to_s | |
big_num = big_number[number_string.length] | |
if self < 20 | |
first_twenty[number_string] | |
elsif self < 100 | |
tens[number_string[0]] + "-" + first_twenty[number_string[1]] | |
elsif self < 1000 | |
if number_string[1].to_i == 1 | |
exception_1 = number_string[1] << number_string[2] | |
number_string[0].to_i.in_words + big_num + exception_1.to_i.in_words | |
elsif number_string[2].to_i == 0 | |
number_string[0].to_i.in_words + big_num + number_string[1].to_i.in_words | |
else | |
number_string[0].to_i.in_words + big_num + number_string[1..2].to_i.in_words | |
end | |
elsif self < 10000 | |
number_string[0].to_i.in_words + big_num + " " + number_string[1..self].to_i.in_words | |
elsif self < 100000 | |
number_string[0..1].to_i.in_words + big_num + " " + number_string[2..self].to_i.in_words | |
elsif self < 1000000 | |
number_string[0..2].to_i.in_words + big_num + " " + number_string[3..self].to_i.in_words | |
elsif self < 10000000 | |
number_string[0].to_i.in_words + big_num + " " + number_string[1..self].to_i.in_words | |
elsif self < 100000000 | |
number_string[0..1].to_i.in_words + big_num + " " + number_string[2..self].to_i.in_words | |
elsif self < 1000000000 | |
number_string[0..2].to_i.in_words + big_num + " " + number_string[3..self].to_i.in_words | |
elsif self < 10000000000 | |
number_string[0].to_i.in_words + big_num + " " + number_string[1..self].to_i.in_words | |
elsif self < 100000000000 | |
number_string[0..1].to_i.in_words + big_num + " " + number_string[2..self].to_i.in_words | |
elsif self < 1000000000000 | |
number_string[0..2].to_i.in_words + big_num + " " + number_string[3..self].to_i.in_words | |
elsif self < 10000000000000 | |
number_string[0].to_i.in_words + big_num + " " + number_string[1..self].to_i.in_words | |
elsif self < 100000000000000 | |
number_string[0..1].to_i.in_words + big_num + " " + number_string[2..self].to_i.in_words | |
elsif self < 1000000000000000 | |
number_string[0..2].to_i.in_words + big_num + " " + number_string[3..self].to_i.in_words | |
end | |
end | |
end | |
#unit tests | |
puts "'#{0.in_words}' should be 'Zero'" | |
puts "'#{4.in_words}' should be 'Four'" | |
puts "'#{9.in_words}' should be 'Nine'" | |
puts "'#{13.in_words}' should be 'Thirteen'" | |
puts "'#{22.in_words}' should be 'Twenty-Two'" | |
puts "'#{37.in_words}' should be 'Thirty-Seven'" | |
puts "'#{137.in_words}' should be 'One-Hundred and Thirty-Seven'" | |
puts "'#{287.in_words}' should be 'Two-Hundred and Eighty-Seven'" | |
puts "'#{396.in_words}' should be 'Three-Hundred and Ninety-Six'" | |
puts "'#{420.in_words}' should be 'Four-Hundred and Twenty'" | |
puts "'#{511.in_words}' should be 'Five-Hundred and Eleven'" | |
puts "'#{613.in_words}' should be 'Six-Hundred and Thirteen'" | |
puts "'#{750.in_words}' should be 'Seven-Hundred and Fifty'" | |
puts "'#{819.in_words}' should be 'Eight-Hundred and Nineteen'" | |
puts "'#{999.in_words}' should be 'Nine-Hundred and Ninety-Nine'" | |
puts "'#{1248.in_words}' should be 'One-Thousand Two-Hundred and Fourty-Eight'" | |
puts "'#{14248.in_words}' should be 'Fourteen-Thousand Two-Hundred and Fourty-Eight'" | |
puts "'#{278248.in_words}' should be 'Two-Hundred and Seventy-Eight-Thousand Two-Hundred and Fourty-Eight'" | |
puts "'#{8748353.in_words}' should be 'Eight-Million Seven-Hundred and Fourty-Eight-Thousand Eight-Hundred and Fifty-Three'" | |
puts "'#{24874833.in_words}' should be 'Twenty-Four-Million Eight-Hundred and Seventy-Four-Thousand Eight-Hundred and Thirty-Three'" | |
puts "'#{193059395.in_words}' should be 'Nine-Hundred and Ninety-Nine'" | |
puts "'#{1948374629.in_words}' should be 'Nine-Hundred and Ninety-Nine'" | |
puts "'#{19385730926.in_words}' should be 'Nine-Hundred and Ninety-Nine'" | |
puts "'#{893059939209.in_words}' should be 'Nine-Hundred and Ninety-Nine'" | |
puts "'#{2782487483953.in_words}' should be 'Nine-Hundred and Ninety-Nine'" | |
puts "'#{27824487483953.in_words}' should be 'Nine-Hundred and Ninety-Nine'" | |
puts "'#{278584874839533.in_words}' should be 'Nine-Hundred and Ninety-Nine'" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment