Skip to content

Instantly share code, notes, and snippets.

@mindplace
Created March 29, 2016 17:33
Show Gist options
  • Save mindplace/da5ada50c5c0643ca65a57d72705e2b5 to your computer and use it in GitHub Desktop.
Save mindplace/da5ada50c5c0643ca65a57d72705e2b5 to your computer and use it in GitHub Desktop.
def into_words(num)
ones = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
teens = ["eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
tens = ["ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
if num == 0
return "zero"
elsif num % 10 == 0 && num < 100
return tens[(num / 10) - 1]
elsif num < 20
return teens[(num - 10) - 1] if num > 10
return ones[(num - 1)]
end
if num < 100
tens_place = num.to_s[0].to_i - 1
ones_place = num.to_s[1].to_i - 1
return "#{tens[tens_place]}-#{ones[ones_place]}"
end
if num < 1000
hundreds = num.to_s[0].to_i
hundreds = "#{into_words(hundreds)} hundred"
tens_place = num.to_s[1..-1].to_i
if tens_place > 0
tens_place = "#{into_words(tens_place)}"
return "#{hundreds} and #{tens_place}"
else
return hundreds
end
end
if num < 1000000
thousands = num.to_s.reverse[3..-1].reverse.to_i
hundreds = num.to_s.reverse[0..2].reverse.to_i
if hundreds > 0
return "#{into_words(thousands)} thousand, #{into_words(hundreds)}"
else
return "#{into_words(thousands)} thousand"
end
end
end
# Tests
puts into_words(187) == "one hundred and eighty-seven"
puts into_words(998) == "nine hundred and ninety-eight"
puts into_words(806) == "eight hundred and six"
puts into_words(23895) == "twenty-three thousand, eight hundred and ninety-five"
puts into_words(762901) == "seven hundred and sixty-two thousand, nine hundred and one"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment