Skip to content

Instantly share code, notes, and snippets.

@pzaich
Created June 15, 2012 06:59
Show Gist options
  • Select an option

  • Save pzaich/2935097 to your computer and use it in GitHub Desktop.

Select an option

Save pzaich/2935097 to your computer and use it in GitHub Desktop.
In Words to 999
module InWords
def in_words
num_array = self.to_s.split("")
num_array.collect! { |x| x.to_i }
hundreds = ""
if num_array.length == 3
hundreds = "#{numbers_hash[num_array.first]} hundred "
end
remainder = self % 100
remainder < 20 ? "#{hundreds + numbers_hash[remainder]}" : "#{hundreds + numbers_hash[num_array[-2] * 10]} #{numbers_hash[num_array[-1]]}"
end
def tens(digit)
numbers_hash[digit]
end
def numbers_hash
{ 0 => "", 1 => "one", 2 => "two", 3 => "three", 4 => "four", 5 => "five", 6 => "six", 7 => "seven", 8 => "eight", 9 => "nine",
10 => "ten", 11 => "eleven", 12 => "twelve", 13 => "thirteen", 14 => "fourteen", 15 => "fifteen", 16 => "sixteen", 17 => "seventeen",
18 => "eighteen", 19 => "nineteen", 20 => "twenty", 30 => "thirty", 40 => "forty", 50 => "fifty", 60 => "sixty", 70 => "seventy",
80 => "eighty", 90 =>"ninety"}
end
end
class Numeric
include InWords
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment