Skip to content

Instantly share code, notes, and snippets.

@perspectivezoom
Created June 12, 2012 22:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perspectivezoom/2920667 to your computer and use it in GitHub Desktop.
Save perspectivezoom/2920667 to your computer and use it in GitHub Desktop.
module InWords
def in_words()
ones_array = %w{ not_called one two three four five six seven eight nine ten}
tens_array = %w{ not_called not_called twenty thirty forty fifty sixty seventy eighty ninety}
teens_array = [ "" ] + %w{ one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen}
if self == 0
return "zero"
end
hundreds = self / 100
two_digit = self % 100
if two_digit < 20
tens_word = teens_array[two_digit]
else
tens = two_digit / 10
ones = two_digit % 10
tens_word = tens_array[tens]
tens_word << " " + ones_array[ones] unless ones == 0
end
if hundreds == 0
tens_word
else
hundreds_word = ones_array[hundreds] + " hundred"
hundreds_word << " " + tens_word unless two_digit == 0
hundreds_word
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