Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Number to Word Converter - Java Code Bank


Number to Word Converter
This is a rewrite to newbee's word_converter_fn program. This takes up 999 decillion and prints out the worded equivalent.
                /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package word_converter_fn;

/**
 *
 * @author elmiguel and selliser
 */
public class Word_converter_fn {
   private static String ones[]     = {"one","two","three","four","five","six","seven","eight","nine"}; 
   private static String elevens[]  = {"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"}; 
   private static String tens[]     = {"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
   private static String decimalGrouping[] = {"thousand", "million", "billion", "trillion", "quadrillion", "sextillion", "septillion", "octillion","nonillion", "decillion"};   
   
   public static String calculateTriple(int number){
       //check for even group values (i.e. 1,000)
       if(number==0){
       return "";
       }
      String tripleString="";
      int localCalc=number%100; 
      if (number>99){
           tripleString+=convertOnes(number/100)+" hundred ";
       }
      //x Modulo 100 gives us the right two digits.
      //Check for Elevens first
      if(localCalc<20 && localCalc>=10){
          tripleString+=convertElevens(localCalc);
      }else{
          //not an eleven; calc tens then ones
          if(localCalc>19){
           tripleString+=convertTens(localCalc)+" ";
          }
          //always calc ones
          tripleString+=convertOnes(localCalc%10);
      }
       return tripleString;
   }
   public static String calculateGroup(int number){
       return decimalGrouping[number];
   }
   public static String convertOnes(int number){
       if(number>0){
        return ones[number-1];    
       }
       else{
           return "";
       }
   }
   public static String convertElevens(int number){
       return elevens[(number%10)];
   }
   public static String convertTens(int number){
       return tens[number/10 -2];//-2 is the normal -1 offset plus
                                // an additoinal -1 for removing "ten"
   }

  public static String convert(String n) { 
    String out = "";
    String tripleNum="";
    
    if (n.equals("0")){
        return "zero";
    }//zero check
    //Zero pad the front of the string 
    while(n.length()%3!=0){

        n="0"+n;
    }
    //First check for elevens becuase they don't follow
    //nomenclature rules
    int decimalGroupIndex=n.length()/3;
    for(int i=0;i<n.length()/3;i++){        
        if(i>0){
         out+= " " + decimalGrouping[(decimalGroupIndex-i)-1] + " ";
        }
        //Break the number into three-digit intervals.
        tripleNum=n.substring(i*3, (i*3)+3);
        out+= calculateTriple(Integer.parseInt(tripleNum));
    }//loop

      return out; 
      }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
          
            for(String arg : args){
                    System.out.println("input : " + arg + "\r\nOutput : " + convert(arg).trim());
            }
    }
}

            
Comments
Sorry but there are no comments to display