Created
June 12, 2012 22:56
-
-
Save perspectivezoom/2920667 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
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