Created
March 17, 2009 11:45
-
-
Save oscardelben/80483 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 number_under_1000_to_s(n) | |
if n == 1000 | |
'onethousand' | |
else | |
number_from_999_to_100_to_s(n) | |
end | |
end | |
def number_from_999_to_100_to_s(n) | |
if n > 99 | |
appendix = number_under_9_to_s( n.first_digit ) | |
result = "#{appendix}hundred" | |
if n % 100 != 0 # need to calculate and... | |
result = result + 'and' + number_from_99_to_10_to_s( n.except_first_digit ) | |
end | |
result | |
else | |
number_from_99_to_10_to_s(n) | |
end | |
end | |
def number_from_99_to_10_to_s(n) | |
if n > 9 | |
if n.first_digit == 1 | |
number_from_19_to_10(n) | |
else | |
result = first_decimal_digit( n.first_digit ) | |
if n % 10 != 0 | |
result = result + number_under_9_to_s( n.except_first_digit ) | |
end | |
result | |
end | |
else | |
number_under_9_to_s(n) | |
end | |
end | |
def number_from_19_to_10(n) | |
case n | |
when 19: 'nineteen' | |
when 18: 'eighteen' | |
when 17: 'seventeen' | |
when 16: 'sixteen' | |
when 15: 'fifteen' | |
when 14: 'fourteen' | |
when 13: 'thirteen' | |
when 12: 'twelve' | |
when 11: 'eleven' | |
when 10: 'ten' | |
end | |
end | |
def first_decimal_digit(n) | |
case n | |
when 9: 'ninety' | |
when 8: 'eigthy' | |
when 7: 'seventy' | |
when 6: 'sixty' | |
when 5: 'fifty' | |
when 4: 'forty' | |
when 3: 'thirty' | |
when 2: 'twenty' | |
end | |
end | |
def number_under_9_to_s(n) | |
case n | |
when 9: 'nine' | |
when 8: 'eigth' | |
when 7: 'seven' | |
when 6: 'six' | |
when 5: 'five' | |
when 4: 'four' | |
when 3: 'three' | |
when 2: 'two' | |
when 1: 'one' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment