Skip to content

Instantly share code, notes, and snippets.

@abrongersma
Created April 4, 2013 00:40
Show Gist options
  • Select an option

  • Save abrongersma/5306756 to your computer and use it in GitHub Desktop.

Select an option

Save abrongersma/5306756 to your computer and use it in GitHub Desktop.
module InWords
require 'enumerator'
ONES= %w{one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen}.unshift("")
TENS = %w{twenty thirty forty fifty sixty seventy eighty ninety}.unshift("", "")
def in_words
r = ""
if self > 99
r = ONES[self/100] + " hundred " + (self%100).in_words
elsif self >= 20
r = TENS[self/10] + " " + (self%10).in_words
else
r = ONES[self]
end
r ? r.strip : ""
end
end
class Numeric
include InWords
end
module InWords
SMALL_NUMBERS = %w{zero one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen}
TENS = %w{zero ten twenty thirty forty fifty sixty seventy eighty ninety}
def in_words
# convert to string to easily grab digits
self_string = self.to_s
# for the digits that don't exist they will be nil
# nil.to_i = 0
h = self_string[-3].to_i
t = self_string[-2].to_i
o = self_string[-1].to_i
words = []
# hundred
words << SMALL_NUMBERS[h.to_i] + " hundred" if h != 0
# teens
if t == 1
words << SMALL_NUMBERS[self_string[-2..-1].to_i]
# non-teen
else
words << TENS[t] if t != 0
words << SMALL_NUMBERS[o.to_i] if o != 0
end
words.join(" ")
end
end
class Fixnum
include InWords
end
SMALL_NUMBERS = %w{one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen}
TENS = %w{ten twenty thirty forty fifty sixty seventy eighty ninety}
def in_words_old_2
return "n/a" if self > 999
# to get an array that has [ones,tens,hundreds]
array = self.to_s.split('').reverse
parts = []
if self > 99
parts << SMALL_NUMBERS[array[2].to_i - 1] + " hundred"
array.pop
end
if self > 19 && array[0..1].reverse.join.to_i > 19
parts << TENS[array[1].to_i - 1]
parts << SMALL_NUMBERS[array[0].to_i - 1]
else
parts << SMALL_NUMBERS[array[0..1].reverse.join.to_i - 1]
end
parts.join(" ")
end
def in_words_old
return "n/a" if self > 999
num = []
h = self - (self%100)
t = self - h - (self%10)
o = self - h - t - (self%1)
# puts "#{self} h:#{h} t:#{t} o:#{o}"
if h > 0
# 100-900 - all hundred-digits are the same
num << (%w{one two three four five six seven eight nine}[h/100 - 1] + " hundred")
end
if t > 20
# 20-90 - all ten-digits are the same
num << %w{twenty thirty forty fifty sixty seventy eighty ninety}[t/10 - 2]
end
if (t+o) < 20
# 1-19 - all unqiue, need to just look up
num << %w{one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen}[(t+o) -1]
else
# 1-9
num << %w{one two three four five six seven eight nine}[o -1]
end
num.join(" ")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment