Skip to content

Instantly share code, notes, and snippets.

@hosiawak
Created November 21, 2011 19:01
Show Gist options
  • Save hosiawak/1383565 to your computer and use it in GitHub Desktop.
Save hosiawak/1383565 to your computer and use it in GitHub Desktop.
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