Created
August 31, 2012 20:51
-
-
Save mikesjewett/3558818 to your computer and use it in GitHub Desktop.
in_words
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
module InWords | |
def in_words | |
return "n/a" if self > 999 | |
num = [] | |
h_num = self - (self%100) | |
t_num = self - h_num - (self%10) | |
o_num = self - h_num - t_num - (self%1) | |
h_words = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] | |
t_words = ["twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"] | |
t_o_words = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", | |
"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", | |
"eighteen", "nineteen"] | |
o_words = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] | |
if h_num > 0 | |
# 100-900 - all hundred-digits are the same | |
num << h_words[h_num/100 - 1] + " hundred" | |
end | |
if t_num > 20 | |
# 20-90 - all ten-digits are the same | |
num << t_words[t_num/10 - 2] | |
end | |
if (t_num+o_num) < 20 | |
# 1-19 - all unqiue, need to just look up | |
num << t_o_words[(t_num+o_num) -1] | |
else | |
# 1-9 | |
num << o_words[o_num -1] | |
end | |
num.join(" ") | |
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