Created
November 21, 2011 19:01
-
-
Save hosiawak/1383565 to your computer and use it in GitHub Desktop.
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
def to_words(num) | |
a0 = %w{ one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen } | |
a1 = %w{ twenty thirty forty fifty sixty seventy eighty ninety } | |
case num | |
when 1..19 | |
a0[num-1] | |
when 20..99 | |
num % 10 != 0 ? "#{a1[(num-20)/10]}#{a0[num % 10 - 1]}" : a1[(num-20)/10] | |
when 100..999 | |
num % 100 != 0 ? "#{to_words(num/100)}hundredand#{to_words(num % 100)}" : "#{to_words(num/100)}hundred" | |
when 1000 | |
"onethousand" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment