Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 7th, 2012  |  syntax: None  |  size: 3.36 KB  |  hits: 5  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. require 'rspec'
  2.  
  3. module InWords
  4.  
  5.   def in_words
  6.     #conditional statements
  7.     a = if self == 1_000_000_000_000
  8.       "one trillion"
  9.     elsif self >= 1_000_000_000    #self is greater to or equal to 1 Billion
  10.       prefix = self / 1_000_000_000
  11.       suffix = self - (prefix * 1_000_000_000)
  12.       prefix.in_words_by_100 + " billion " + suffix.in_words
  13.     elsif self >= 1_000_000   #self is greater to or equal to 1 Million
  14.       prefix = self / 1_000_000
  15.       suffix = self - (prefix * 1_000_000)
  16.       prefix.in_words_by_100 + " million " + suffix.in_words
  17.     elsif self >= 1_000   #self is greater to or equal to 1 thousand
  18.       prefix = self / 1_000
  19.       suffix = self - (prefix * 1_000)
  20.       prefix.in_words_by_100 + " thousand " + suffix.in_words
  21.     else
  22.       self.in_words_by_100
  23.     end
  24.     a.split.join(' ')
  25.   end
  26.  
  27.   def in_words_by_100()
  28.     single= {0=>"",1=>"one",2=>"two",3=>"three",4=>"four",5=>"five",6=>"six", 7=>"seven",8=>"eight",9=>"nine"}
  29.     teens = {11=>"eleven",12=>"twelve",13=>"thirteen",14=>"fourteen",15=>"fifteen",16=>"sixteen", 17=>"seventeen",18=>"eighteen",19=>"nineteen"}
  30.     tens = {0=>"",1=>"ten",2=>"twenty",3=>"thirty",4=>"forty",5=>"fifty",6=>"sixty", 7=>"seventy",8=>"eighty",9=>"ninety"}
  31.  
  32.     if self<10
  33.       single[self]
  34.     elsif self.between?(11,19)
  35.       teens[self]
  36.     elsif self.between?(19,99)
  37.       tens[self/10] + " " + single[self%10]
  38.     elsif self%100 < 10 && self< 1000
  39.       single[self/100] + " hundred " + single[self%100]
  40.     elsif self%100 < 20 && self < 1000
  41.       single[self/100] + " hundred " + teens[self%100]
  42.     elsif self%100 >= 20 && self < 1000
  43.       single[self/100] + " hundred " + tens[(self/10)%10] + " " + single[(self%100)%10]
  44.     else
  45.       puts self
  46.       puts "Error"
  47.     end
  48.   end
  49. end
  50.  
  51. class Fixnum
  52.   include InWords
  53. end
  54.  
  55. describe Fixnum do
  56.   describe "in_words" do
  57.     it "translates numbers less than ten" do
  58.       4.in_words.should == "four"
  59.     end
  60.     it "translates the teens" do
  61.       17.in_words.should == "seventeen"
  62.     end
  63.     it "translates numbers 20 through 99" do
  64.       47.in_words.should == "forty seven"
  65.       70.in_words.should == "seventy"
  66.       77.in_words.should == "seventy seven"
  67.     end
  68.     it "translates numbers 100 through 999" do
  69.       100.in_words.should == "one hundred"
  70.       206.in_words.should == "two hundred six"
  71.       613.in_words.should == "six hundred thirteen"
  72.       992.in_words.should == "nine hundred ninety two"
  73.     end
  74.     it "translates one thousand up to one million" do
  75.       1_000.in_words.should == "one thousand"
  76.       1_849.in_words.should == "one thousand eight hundred forty nine"
  77.       15_937.in_words.should == "fifteen thousand nine hundred thirty seven"
  78.       352_947.in_words.should == "three hundred fifty two thousand nine hundred forty seven"
  79.     end
  80.     it "translates numbers one million up to one billion" do
  81.      # 1_000_000.in_words.should == "one million"
  82.       573_917_408.in_words.should == "five hundred seventy three million nine hundred seventeen thousand four hundred eight"
  83.     end
  84.     it "translates numbers one billion up to one trillion" do
  85.       1_000_000_000.in_words.should == "one billion"
  86.       719_471_920_483.in_words.should == "seven hundred nineteen billion four hundred seventy one million nine hundred twenty thousand four hundred eighty three"
  87.     end
  88.     it "translates one trillion" do
  89.       1_000_000_000_000.in_words.should == "one trillion"
  90.     end
  91.   end
  92. end