I m fade up with searching the code to convert number to word in ruby .I found several of them but nothing worked right.please help me -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
class Fixnum def english_word @h = {0=>"zero", 1=>"One", 2=>"Two", 3=>"Three", 4=>"Four", 5=>"Five",6=>"six", 7=>"seven", 8=>"Eight", 9=>"Nine",10=>"Ten",11=>"Eleven",12=>"Twelve", 13=>"Thirteen",14=>"Fourteen",15=>"Fifteen", 16=>"Sixteen",17=>"Seventeen",18=>"Eighteen", 19=>"Nineteen",20=>"Twenty",30=>"Thirty", 40=>"Fourty",50=>"Fifty",60=>"Sixty",70=>"Seventy", 80=>"Eighty",90=>"Ninty"} @i=0 @array=[] @result=""a if self > 99 str_num=self.to_s #@num.to_s str_num_len=str_num.length str_full_num=str_num.insert(0,"0"*(11-str_num_len)) str_full_num=str_num.insert(8,"0") str_full_num.scan(/../) { |x| @array<<x } 6.times do self.def_calc @i+=1 end else if self > 9 puts (self.proc_double_dig((self/10)*10))+ (self.proc_single_dig(self%10)) else if self > 0 puts self.proc_single_dig(self) else return "AMOUNT NOT KNOWN or NILL" end end end end def def_calc case @i when 0 str=self.proc_unit(@array[@i]) if (str.scan(/\w+/)).length!=0 then str=str+ "hundred & " @result=@result+str end when 1 str=self.proc_unit(@array[@i]) if (str.scan(/\w+/)).length!=0 then str=str+ " Crore, " @result=@result+str end when 2 str=self.proc_unit(@array[@i]) if (str.scan(/\w+/)).length!=0 then str=str+ " Lakh, " @result=@result+str end when 3 str=self.proc_unit(@array[@i]) if (str.scan(/\w+/)).length!=0 then str=str+ " Thousand, " @result=@result+str end when 4 str=self.proc_unit(@array[@i]) if (str.scan(/\w+/)).length!=0 then str=str+ " Hundred, " @result=@result+str end when 5 str=self.proc_unit(@array[@i]) if (str.scan(/\w+/)).length!=0 then str=str+ ". " @result=@result+str end print @result.sub(/..$/,"") else end end def proc_unit(x) if x.to_i>0 if x.to_i<=10 return self.proc_single_dig(x.to_i) else if x.to_i<=20 return self.proc_double_dig(x.to_i) else return (self.proc_double_dig((x.to_i/10)*10))+ (self.proc_single_dig(x.to_i%10)) end end end return "" end def proc_double_dig(z) if z==0 return "" else return @h[z] end end def proc_single_dig(y) if y==0 return "" else return @h[y] end end protected :def_calc, :proc_unit, :proc_double_dig, :proc_single_dig end puts 453645445.english_word #FourtyFive Crore, Thirtysix Lakh, FourtyFive Thousand, Four Hundred,FourtyFive On Fri, Dec 4, 2009 at 5:56 PM, INDRANIL MUKHERJEE < indranil.since87-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I m fade up with searching the code to convert number to word in > ruby .I found several of them but nothing worked right.please help me > > -- > > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Check out Chris Pine''s Book -- Learn to Program from the Pragmatic Programmers He has an English Number example in there: http://pine.fm/LearnToProgram/?Chapter=08 def englishNumber number if number < 0 # No negative numbers. return ''Please enter a number that isn\''t negative.'' end if number == 0 return ''zero'' end # No more special cases! No more returns! numString = '''' # This is the string we will return. onesPlace = [''one'', ''two'', ''three'', ''four'', ''five'', ''six'', ''seven'', ''eight'', ''nine''] tensPlace = [''ten'', ''twenty'', ''thirty'', ''forty'', ''fifty'', ''sixty'', ''seventy'', ''eighty'', ''ninety''] teenagers = [''eleven'', ''twelve'', ''thirteen'', ''fourteen'', ''fifteen'', ''sixteen'', ''seventeen'', ''eighteen'', ''nineteen''] # "left" is how much of the number we still have left to write out. # "write" is the part we are writing out right now. # write and left... get it? :) left = number write = left/100 # How many hundreds left to write out? left = left - write*100 # Subtract off those hundreds. if write > 0 # Now here''s a really sly trick: hundreds = englishNumber write numString = numString + hundreds + '' hundred'' # That''s called "recursion". So what did I just do? # I told this method to call itself, but with "write" instead of # "number". Remember that "write" is (at the moment) the number of # hundreds we have to write out. After we add "hundreds" to "numString", # we add the string '' hundred'' after it. So, for example, if # we originally called englishNumber with 1999 (so "number" 1999), # then at this point "write" would be 19, and "left" would be 99. # The laziest thing to do at this point is to have englishNumber # write out the ''nineteen'' for us, then we write out '' hundred'', # and then the rest of englishNumber writes out ''ninety-nine''. if left > 0 # So we don''t write ''two hundredfifty-one''... numString = numString + '' '' end end write = left/10 # How many tens left to write out? left = left - write*10 # Subtract off those tens. if write > 0 if ((write == 1) and (left > 0)) # Since we can''t write "tenty-two" instead of "twelve", # we have to make a special exception for these. numString = numString + teenagers[left-1] # The "-1" is because teenagers[3] is ''fourteen'', not ''thirteen''. # Since we took care of the digit in the ones place already, # we have nothing left to write. left = 0 else numString = numString + tensPlace[write-1] # The "-1" is because tensPlace[3] is ''forty'', not ''thirty''. end if left > 0 # So we don''t write ''sixtyfour''... numString = numString + ''-'' end end write = left # How many ones left to write out? left = 0 # Subtract off those ones. if write > 0 numString = numString + onesPlace[write-1] # The "-1" is because onesPlace[3] is ''four'', not ''three''. end # Now we just return "numString"... numString end puts englishNumber( 0) puts englishNumber( 9) puts englishNumber( 10) puts englishNumber( 11) puts englishNumber( 17) puts englishNumber( 32) puts englishNumber( 88) puts englishNumber( 99) puts englishNumber(100) puts englishNumber(101) puts englishNumber(234) puts englishNumber(3211) puts englishNumber(999999) puts englishNumber(1000000000000) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.