Skip to content

Instantly share code, notes, and snippets.

@istro
Created June 15, 2012 01:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save istro/2934091 to your computer and use it in GitHub Desktop.
Save istro/2934091 to your computer and use it in GitHub Desktop.
in_words in progress
module InWords
def in_words
places=1
num = self
while(num/10 != 0)
places num+= 1
num = num/10
end
if self>1000
"sorry, I can't count higher then 1000!"
else
small =['zero', 'one', 'two', 'three', 'four', 'five',
'six', 'seven', 'eight', 'nine', 'ten',
'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen',
'sixteen', 'seventeen', 'eighteen', 'nineteen']
tens = ['zero', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
more = ['hundred', 'thousand', 'million', 'billion', 'trillion']
case size
when places=1
puts small[self]
when places = 2
if self<20
puts small[self]
else
puts tens[self/10]+" "+small[self%10]
end
when places = 3
puts self[self/100]+" "+more[0]+tens[self%100/10]+" "+small[self%10]
end
end
end
end
class Fixnum
include InWords
end
# tests
describe InWords do
it "has an in_words method" do
class TestClass
include InWords
end
TestClass.new.should respond_to(:in_words)
end
end
describe Fixnum do
describe "in_words" do
it "translates numbers less than ten" do
4.in_words.should eq("four")
end
it "translates the teens" do
17.in_words.should eq("seventeen")
end
it "translates numbers 20 through 99" do
47.in_words.should eq("forty seven")
end
it "translates numbers 100 through 999" do
206.in_words.should eq("two hundred six")
613.in_words.should eq("six hundred thirteen")
992.in_words.should eq("nine hundred ninety two")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment