Created
June 14, 2012 03:40
-
-
Save jessieay/2927883 to your computer and use it in GitHub Desktop.
Ugly ugly version of all integers up to 1000 in words
This file contains hidden or 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
module InWords | |
def in_words | |
num_array = self.to_s.split('') | |
ones_place = {"0" => "", "1" => "one", "2" => "two", "3" => "three", "4" => "four", "5" => "five", "6" => "six", | |
"7" => "seven", "8" => "eight", "9" => "nine"} | |
final_digit_in_words = ones_place[num_array[-1]] | |
tens_place = {"0" => "", "1" => "", "2" => "twenty", "3" => "thirty", "4" => "forty", "5" => "fifty", "6" => "sixty", "7" => "seventy", "8" => "eighty", | |
"9" => "ninety"} | |
second_to_last_digit_in_words = tens_place[num_array[-2]] | |
hundreds_place = {"1" => "one hundred", "2" => "two hundred", "3" => "three hundred", "4" => "four hundred", "5" => "five hundred", "6" => "six hundred", | |
"7" => "seven hundred", "8" => "eight hundred", "9" => "nine hundred"} | |
third_to_last_digit_in_words = hundreds_place[num_array[-3]] | |
teens_array = {"10" => "ten", "11" => "eleven", "12" => "twelve", "13" => "thirteen", "14" => "fourteen", | |
"15" => "fifteen", "16" => "sixteen", "17" => "seventeen", "18" => "eighteen", "19" => "nineteen"} | |
if num_array.length == 4 | |
return "one thousand" | |
elsif num_array[0] == "1" && num_array.length == 2 | |
return teens_array[num_array.join] | |
elsif num_array[1] == "1" && num_array.length == 3 | |
last_two = num_array[1..2] | |
return third_to_last_digit_in_words.to_s + " " + teens_array[last_two.join].to_s | |
elsif num_array[1] == "0" && num_array.length == 3 | |
return third_to_last_digit_in_words + " " + final_digit_in_words | |
elsif num_array.length == 2 | |
return "#{second_to_last_digit_in_words} #{final_digit_in_words}" | |
elsif num_array.length == 1 | |
return final_digit_in_words | |
else | |
return "#{third_to_last_digit_in_words} #{second_to_last_digit_in_words} #{final_digit_in_words}" | |
end | |
end | |
end | |
class Fixnum | |
include InWords | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment