About NewTechnoBuzz
Advertise
Contact Us

Wednesday, August 6, 2014

How to convert number to words in java

This question was asked from in an interview (Java interview questions). But, the mode of asking the question was different. The interviewer asked the question like this Write a program to check that fifty is equal to 50..

I was able to give the answer of this question, but it was not an optimized one. After that, I tried to find out the solutions of this problem and found some solutions.

The solution of this problem is as below:

package com;
import java.text.DecimalFormat;

public class TestJava {

   private static final String[] tensNames = {
      "",
      " ten",
      " twenty",
      " thirty",
      " forty",
      " fifty",
      " sixty",
      " seventy",
      " eighty",
      " ninety"
   };

   private static final String[] numNames = {
      "",
      " one",
      " two",
      " three",
      " four",
      " five",
      " six",
      " seven",
      " eight",
      " nine",
      " ten",
      " eleven",
      " twelve",
      " thirteen",
      " fourteen",
      " fifteen",
      " sixteen",
      " seventeen",
      " eighteen",
      " nineteen"
   };

   private TestJava() {}

   private static String convertNumberLessThanOneThousand(int number) {
      String str;

      if (number % 100 < 20){
         str = numNames[number % 100];
         number /= 100;
      }
      else {
         str = numNames[number % 10];
         number /= 10;

         str = tensNames[number % 10] + str;
         number /= 10;
      }
      if (number == 0) return str;
         return numNames[number] + " hundred" + str;
   }

   public static String convert(long number) {

      if (number == 0) { return "zero"; }

      String strNumber = Long.toString(number);

      // pad with "0"
      String mask = "000000000000";
      DecimalFormat df = new DecimalFormat(mask);
      strNumber = df.format(number);

      // XXXnnnnnnnnn
      int billions = Integer.parseInt(strNumber.substring(0,3));
      // nnnXXXnnnnnn
      int millions  = Integer.parseInt(strNumber.substring(3,6));
      // nnnnnnXXXnnn
      int hundredThousands = Integer.parseInt(strNumber.substring(6,9));
      // nnnnnnnnnXXX
      int thousands = Integer.parseInt(strNumber.substring(9,12));

      String tradBillions;
      switch (billions) {
         case 0:
            tradBillions = "";
            break;
         case 1 :
            tradBillions = convertNumberLessThanOneThousand(billions)
            + " billion ";
            break;
         default :
            tradBillions = convertNumberLessThanOneThousand(billions)
            + " billion ";
      }
      String result =  tradBillions;

      String tradMillions;
      switch (millions) {
         case 0:
            tradMillions = "";
            break;
         case 1 :
            tradMillions = convertNumberLessThanOneThousand(millions)
            + " million ";
            break;
         default :
            tradMillions = convertNumberLessThanOneThousand(millions)
            + " million ";
      }
      result =  result + tradMillions;

      String tradHundredThousands;
      switch (hundredThousands) {
         case 0:
            tradHundredThousands = "";
            break;
         case 1 :
            tradHundredThousands = "one thousand ";
            break;
         default :
            tradHundredThousands = convertNumberLessThanOneThousand(hundredThousands)
            + " thousand ";
      }
      result =  result + tradHundredThousands;

      String tradThousand;
      tradThousand = convertNumberLessThanOneThousand(thousands);
      result =  result + tradThousand;

      // remove extra spaces!
      return result.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " ");
   }

   /**
   * testing
   * @param args
   */
   public static void main(String[] args) {
      System.out.println("*** " + TestJava.convert(0));
      System.out.println("*** " + TestJava.convert(1));
      System.out.println("*** " + TestJava.convert(16));
      System.out.println("*** " + TestJava.convert(100));
      System.out.println("*** " + TestJava.convert(118));
      System.out.println("*** " + TestJava.convert(200));
      System.out.println("*** " + TestJava.convert(219));
      System.out.println("*** " + TestJava.convert(800));
      System.out.println("*** " + TestJava.convert(801));
      System.out.println("*** " + TestJava.convert(1316));
      System.out.println("*** " + TestJava.convert(1000000));
      System.out.println("*** " + TestJava.convert(2000000));
      System.out.println("*** " + TestJava.convert(3000200));
      System.out.println("*** " + TestJava.convert(700000));
      System.out.println("*** " + TestJava.convert(9000000));
      System.out.println("*** " + TestJava.convert(9001000));
      System.out.println("*** " + TestJava.convert(123456789));
      System.out.println("*** " + TestJava.convert(2147483647));
      System.out.println("*** " + TestJava.convert(3000000010L));
   }
}

Output:

*** zero
*** one
*** sixteen
*** one hundred
*** one hundred eighteen
*** two hundred
*** two hundred nineteen
*** eight hundred
*** eight hundred one
*** one thousand three hundred sixteen
*** one million 
*** two million 
*** three million two hundred
*** seven hundred thousand 
*** nine million 
*** nine million one thousand 
*** one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine
*** two billion one hundred forty seven million four hundred eighty three thousand six hundred forty seven
*** three billion ten


I'll check out more solutions and will update the content. You can provide your comments on the current content.


1 comments