Question:

How convert number to letter in java?

by  |  earlier

0 LIKES UnLike

hi all ,

i have to convert number to letter like this :

if get 25 >> print Twenty five

can any one help me ?:

 Tags:

   Report

3 ANSWERS


  1. a very quick and efficient way would be to store the number's names in an array sequentially :

    example : String numbers [] = new String[100];

    numbers[0] ="zero";

    ....

    However, the correct way would be to use a programmatic approach  :

    NOTE : this is correct for numbers between 0 and 99 inclusive  ONLY ; however; u can expand the algorithm after u understand it if u need to go beyond 99

    String smallNumbers[] = new String[10];

    Now fill it up with the numbers from 0 to 20 manually :

    smallNumbers[0] ="zero";

    smallNumbers[1] ="one";

    smallNumbers[2] ="two";

    ...

    smallNumbers[9] ="nine";

    Now for 11-19 , do it manually :

    String mediumNumbers[] = new String[10];

    mediumNumbers[0] ="ten";

    mediumNumbers[1] ="eleven";

    .....

    mediumNumbers[9] ="nineteen";

    Now   manually fill those slots in the array  who are dividable by 10 :

    String  []  tenMultiples = new String[10];

    tenMultiples[0] ="ten";

    tenMultiples[1] ="twenty";

    tenMultiples[2] ="thirty;

    ...

    tenMultiples[8] ="ninety";

    Now the logic :

    assume the number is :

    int x = 30 ;

    Use this :

    if( x >= 100 ) print "This method is only for numbers between 0 and 99 " ;

    else if ( x >=0 && x <= 9 )      

                  print     smallNumbers[x] ;

    else if ( x >=10 && x <= 19 ) // if the number is  between 10 and 19 inclusive

    {      

      int  y = x % 10 ;

      print  mediumNumbers[ y ] ;

    }

    else if ( x%10 == 0   ) // if the number is a multiple of 10

    {      

      int  y = x % 10 ;

      print  tenMultiples[y -1];

    }

    else //any other number between 0 and 99 besides those mentioned above

    {

      int smallPart = x % 10 ;

      int tensPart = x /10 ;

      

    print "The number is "  tenMultiples [ tensPart-1 ]+ "-" +  smallNumbers[ smallPart ];

    }

    Test the code in the  programming language u r using ( u ain't mentioned it )...i did it in java ; change it into the language ur using ...

    Good luck


  2. I found http://www.pchelpdoc.com

    This site has been helpfull for stuff like this.

    There is alot of free help on there.

    Goodluck!

  3. you COULD put the words in an array. like var name['0','1','2','3','4'] ect.

    then call the variable with your number name[0] = '0'

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.