Created
June 14, 2012 19:11
-
-
Save realdlee/2932280 to your computer and use it in GitHub Desktop.
In Words With Recursion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
module InWords | |
def in_words | |
#conditional statements | |
a = if self == 1_000_000_000_000 | |
"one trillion" | |
elsif self >= 1_000_000_000 #self is greater to or equal to 1 Billion | |
prefix = self / 1_000_000_000 | |
suffix = self - (prefix * 1_000_000_000) | |
prefix.in_words_by_100 + " billion " + suffix.in_words | |
elsif self >= 1_000_000 #self is greater to or equal to 1 Million | |
prefix = self / 1_000_000 | |
suffix = self - (prefix * 1_000_000) | |
prefix.in_words_by_100 + " million " + suffix.in_words | |
elsif self >= 1_000 #self is greater to or equal to 1 thousand | |
prefix = self / 1_000 | |
suffix = self - (prefix * 1_000) | |
prefix.in_words_by_100 + " thousand " + suffix.in_words | |
else | |
self.in_words_by_100 | |
end | |
a.split.join(' ') | |
end | |
def in_words_by_100() | |
single= {0=>"",1=>"one",2=>"two",3=>"three",4=>"four",5=>"five",6=>"six", 7=>"seven",8=>"eight",9=>"nine"} | |
teens = {11=>"eleven",12=>"twelve",13=>"thirteen",14=>"fourteen",15=>"fifteen",16=>"sixteen", 17=>"seventeen",18=>"eighteen",19=>"nineteen"} | |
tens = {0=>"",1=>"ten",2=>"twenty",3=>"thirty",4=>"forty",5=>"fifty",6=>"sixty", 7=>"seventy",8=>"eighty",9=>"ninety"} | |
if self<10 | |
single[self] | |
elsif self.between?(11,19) | |
teens[self] | |
elsif self.between?(19,99) | |
tens[self/10] + " " + single[self%10] | |
elsif self%100 < 10 && self< 1000 | |
single[self/100] + " hundred " + single[self%100] | |
elsif self%100 < 20 && self < 1000 | |
single[self/100] + " hundred " + teens[self%100] | |
elsif self%100 >= 20 && self < 1000 | |
single[self/100] + " hundred " + tens[(self/10)%10] + " " + single[(self%100)%10] | |
else | |
puts self | |
puts "Error" | |
end | |
end | |
end | |
class Fixnum | |
include InWords | |
end | |
describe Fixnum do | |
describe "in_words" do | |
it "translates numbers less than ten" do | |
4.in_words.should == "four" | |
end | |
it "translates the teens" do | |
17.in_words.should == "seventeen" | |
end | |
it "translates numbers 20 through 99" do | |
47.in_words.should == "forty seven" | |
70.in_words.should == "seventy" | |
77.in_words.should == "seventy seven" | |
end | |
it "translates numbers 100 through 999" do | |
100.in_words.should == "one hundred" | |
206.in_words.should == "two hundred six" | |
613.in_words.should == "six hundred thirteen" | |
992.in_words.should == "nine hundred ninety two" | |
end | |
it "translates one thousand up to one million" do | |
1_000.in_words.should == "one thousand" | |
1_849.in_words.should == "one thousand eight hundred forty nine" | |
15_937.in_words.should == "fifteen thousand nine hundred thirty seven" | |
352_947.in_words.should == "three hundred fifty two thousand nine hundred forty seven" | |
end | |
it "translates numbers one million up to one billion" do | |
# 1_000_000.in_words.should == "one million" | |
573_917_408.in_words.should == "five hundred seventy three million nine hundred seventeen thousand four hundred eight" | |
end | |
it "translates numbers one billion up to one trillion" do | |
1_000_000_000.in_words.should == "one billion" | |
719_471_920_483.in_words.should == "seven hundred nineteen billion four hundred seventy one million nine hundred twenty thousand four hundred eighty three" | |
end | |
it "translates one trillion" do | |
1_000_000_000_000.in_words.should == "one trillion" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment