Skip to content

Instantly share code, notes, and snippets.

@colemanfoley
Created September 19, 2012 03:50
Show Gist options
  • Save colemanfoley/3747578 to your computer and use it in GitHub Desktop.
Save colemanfoley/3747578 to your computer and use it in GitHub Desktop.
Numbers into Words
module InWords
#The method for turning integers 1-19 into the appropriate strings.
def under20
array_small_numbers = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
return array_small_numbers[self]
end
#The method for turning integers 20-99 into the appropriate strings.
def under100
first_digit = (self/10).floor
if first_digit == 2
first_part = "twenty"
elsif first_digit == 3
first_part = "three"
elsif first_digit == 4
first_part = "forty"
elsif first_digit == 5
first_part = "five"
elsif first_digit == 6
first_part = "sixty"
elsif first_digit == 7
first_part = "seventy"
elsif first_digit == 8
first_part = "eighty"
elsif first_digit == 9
first_part = "ninety"
end
second_part = (self % 10).under20
return first_part + " " + second_part
end
#This method turns integers between 100 and 999 into words. For the number in the hundreds place, I just passed the number to the under10 method then added "hundred" to the end.
#For the numbers in the tens and ones place, I
def under1000
first_digit = (self/100).floor
first_part = first_digit.under20 + " hundred "
second_part = (self % 100).in_words
return first_part + second_part
end
def in_words
if (self < 20)
return self.under20
end
if (self > 19 && self < 100)
return self.under100
end
if (self > 99 && self < 1000)
return self.under1000
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