Created
June 14, 2012 03:31
-
-
Save perspectivezoom/2927862 to your computer and use it in GitHub Desktop.
This file contains hidden or 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() | |
| if self == 0 | |
| return "zero" | |
| end | |
| lvl_array = %w{ not_called thousand million billion trillion } | |
| max_lvl = (self.to_s.length - 1) / 3 | |
| n = self | |
| ar = [] | |
| 0.upto(max_lvl) do |lvl| | |
| hundreds = n % 1000 | |
| ar.unshift(lvl_array[lvl]) unless hundreds == 0 || lvl == 0 | |
| ar.unshift(hundreds.in_words_hundreds) unless hundreds == 0 | |
| n /= 1000 | |
| end | |
| ar.join(" ") | |
| end | |
| def in_words_hundreds() | |
| 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} | |
| 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