Skip to content

Instantly share code, notes, and snippets.

@phoenixbox
Created March 14, 2013 20:49
Show Gist options
  • Save phoenixbox/5165108 to your computer and use it in GitHub Desktop.
Save phoenixbox/5165108 to your computer and use it in GitHub Desktop.
Say MegaNums
class MegaNums
def initialize(number)
@number = number
end
def say_num
word_nums = ""
if @number > 999_999
word_nums << convert(@number/1_000_000)
word_nums << " million "
end
if @number > 999
word_nums << convert(@number/1000)
word_nums << " thousand "
end
word_nums << convert(@number)
word_nums.strip.squeeze(" ")
end
def low_nums
[""] + %w(one two three four five six seven eight nine
ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen)
end
def decades
%w(singles teens twenty thirty forty fifty sixty seventy eighty ninety)
end
def convert(three_digits)
word_nums = ""
ones = three_digits % 10
tens = three_digits % 100 / 10
hundreds = three_digits % 1000 / 100
if hundreds != 0
word_nums << "#{low_nums[hundreds]} hundred "
end
if tens < 2
word_nums << low_nums[three_digits%100]
else
if tens != 0
word_nums << "#{decades[tens]}"
if ones != 0
word_nums << "-"
end
end
if ones != 0
word_nums << "#{low_nums[ones]}"
end
end
word_nums.strip.squeeze(" ")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment