Skip to content

Convert number to word

02/01/2011

This is the program for convert number to word.

=begin
Program name:number_to_word.rb
Author : sanmugasundaramk
Date of Creation : 02.01.2011
Date of last modification : 02.01.2011
License : GPL
=end
class Getword
  def initialize
    @no_in_word=''
  end
  
  def length(number)
    length=number.to_s.length.to_i
  end
  
  def degit_cutter(number,devide)
    number=number%devide
  end
  
  def get_word(count)
    digit_getter={3 => " hundared and ",4 => " thousand ",5 => " thousand ",6 => " lakhs ",7 => " lakhs ",8 => " crore ",9 =>" crore ",10 =>" million ",11 =>" million ",12 =>" billion ",13 =>" billion ",14 =>" trillion ",15 =>" trillion ",16 =>" zillion ",17 =>" zillion "}
    word=digit_getter[count]
  end
  
  def get_devider(count)
    digit_getter={3 =>100,4 =>1000,5 => 1000,6 => 100000,7 => 100000,8 => 10000000,9 => 10000000,10 =>1000000000,11 =>1000000000,12 =>100000000000,13 =>100000000000,14 =>10000000000000,15 =>10000000000000,16 =>1000000000000000,17 =>1000000000000000 }
    word=digit_getter[count]
  end
  
  def set_last_two_degit(number)
    if (number =20 ) then
      case
        when (number >=20 and number =30 and number =40 and number =50 and number =60 and number =70 and number =80 and number =90 and number < 100) then @no_in_word=@no_in_word+"ninety"
      end
      if (number%10) != 0
        number=number%10
      end
    end
    if (number10) then
      case
        when number==11 then @no_in_word=@no_in_word+"eleven"
        when number==12 then @no_in_word=@no_in_word+"twelve"
        when number==13 then @no_in_word=@no_in_word+"thirteen"
        when number==14 then @no_in_word=@no_in_word+"fourteen"
        when number==15 then @no_in_word=@no_in_word+"fiveteen"
        when number==16 then @no_in_word=@no_in_word+"sixteen"
        when number==17 then @no_in_word=@no_in_word+"seventeen"
        when number==18 then @no_in_word=@no_in_word+"eightteen"
        when number==19 then @no_in_word=@no_in_word+"nineteen"
      end
    end

    if (number>=0 and number2)
      devide=get_devider(length)
      puts devide
      first=number/devide
      puts first
      set_last_two_degit(first)
      @no_in_word=@no_in_word+get_word(length)
      puts @no_in_word
      number=degit_cutter(number,devide)
      length=number.to_s.length
    end
    if length<3
      set_last_two_degit(number)
      puts @no_in_word 
    end
  end
  
end
print "Enter the number:"
number=gets.chomp.to_i
caller=Getword.new
caller.getting_started(number)
Advertisements

From → Ruby

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: