Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)

We currently have a crude mechanism to convert numbers to words and based on the size of the number translating that into an English text. But we are running into issues for numbers that are huge.

10183 = Ten thousand one hundred eighty three

90 = Ninety

5888 = Five thousand eight hundred eighty eight

Is there an easy-to-use function in any of the math libraries which I can use for this purpose?

1 Answer

0 votes
by (13.1k points)

You can do it like this:

import java.text.DecimalFormat;

public class EnglishNumberToWords {

  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 EnglishNumberToWords() {}

  private static String convertLessThanOneThousand(int number) {

    String soFar;

    if (number % 100 < 20){

      soFar = numNames[number % 100];

      number /= 100;

    }

    else {

      soFar = numNames[number % 10];

      number /= 10;

      soFar = tensNames[number % 10] + soFar;

      number /= 10;

    }

    if (number == 0) return soFar;

    return numNames[number] + " hundred" + soFar;

  }


 

  public static String convert(long number) {

    // 0 to 999 999 999 999

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

    String snumber = Long.toString(number);

    // pad with "0"

    String mask = "000000000000";

    DecimalFormat df = new DecimalFormat(mask);

    snumber = df.format(number);

    // XXXnnnnnnnnn

    int billions = Integer.parseInt(snumber.substring(0,3));

    // nnnXXXnnnnnn

    int millions  = Integer.parseInt(snumber.substring(3,6));

    // nnnnnnXXXnnn

    int hundredThousands = Integer.parseInt(snumber.substring(6,9));

    // nnnnnnnnnXXX

    int thousands = Integer.parseInt(snumber.substring(9,12));

    String tradBillions;

    switch (billions) {

    case 0:

      tradBillions = "";

      break;

    case 1 :

      tradBillions = convertLessThanOneThousand(billions)

      + " billion ";

      break;

    default :

      tradBillions = convertLessThanOneThousand(billions)

      + " billion ";

    }

    String result =  tradBillions;

    String tradMillions;

    switch (millions) {

    case 0:

      tradMillions = "";

      break;

    case 1 :

      tradMillions = convertLessThanOneThousand(millions)

         + " million ";

      break;

    default :

      tradMillions = convertLessThanOneThousand(millions)

         + " million ";

    }

    result =  result + tradMillions;

    String tradHundredThousands;

    switch (hundredThousands) {

    case 0:

      tradHundredThousands = "";

      break;

    case 1 :

      tradHundredThousands = "one thousand ";

      break;

    default :

      tradHundredThousands = convertLessThanOneThousand(hundredThousands)

         + " thousand ";

    }

    result =  result + tradHundredThousands;

    String tradThousand;

    tradThousand = convertLessThanOneThousand(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("*** " + EnglishNumberToWords.convert(0));

    System.out.println("*** " + EnglishNumberToWords.convert(1));

    System.out.println("*** " + EnglishNumberToWords.convert(16));

    System.out.println("*** " + EnglishNumberToWords.convert(100));

    System.out.println("*** " + EnglishNumberToWords.convert(118));

    System.out.println("*** " + EnglishNumberToWords.convert(200));

    System.out.println("*** " + EnglishNumberToWords.convert(219));

    System.out.println("*** " + EnglishNumberToWords.convert(800));

    System.out.println("*** " + EnglishNumberToWords.convert(801));

    System.out.println("*** " + EnglishNumberToWords.convert(1316));

    System.out.println("*** " + EnglishNumberToWords.convert(1000000));

    System.out.println("*** " + EnglishNumberToWords.convert(2000000));

    System.out.println("*** " + EnglishNumberToWords.convert(3000200));

    System.out.println("*** " + EnglishNumberToWords.convert(700000));

    System.out.println("*** " + EnglishNumberToWords.convert(9000000));

    System.out.println("*** " + EnglishNumberToWords.convert(9001000));

    System.out.println("*** " + EnglishNumberToWords.convert(123456789));

    System.out.println("*** " + EnglishNumberToWords.convert(2147483647));

    System.out.println("*** " + EnglishNumberToWords.convert(3000000010L));

    /*

     *** 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 millions

     *** three millions two hundred

     *** seven hundred thousand

     *** nine millions

     *** nine millions one thousand

     *** one hundred twenty three millions four hundred

     **      fifty six thousand seven hundred eighty nine

     *** two billion one hundred forty seven millions

     **      four hundred eighty three thousand six hundred forty seven

     *** three billion ten

     **/

  }

}

You can handle “dollar and cent” conversion by calling the “convert” method two times

String phrase = "12345.67" ;

Float num = new Float( phrase ) ;

int dollars = (int)Math.floor( num ) ;

int cent = (int)Math.floor( ( num - dollars ) * 100.0f ) ;

String s = "$ " + EnglishNumberToWords.convert( dollars ) + " and "

           + EnglishNumberToWords.convert( cent ) + " cents" ;

Want to learn Java? Check out the Java certification from Intellipaat. 

Related questions

0 votes
1 answer
0 votes
0 answers
asked Feb 18, 2021 in Java by Harsh (1.5k points)
0 votes
1 answer
asked Feb 11, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
asked Feb 8, 2021 in Java by dante07 (13.1k points)
+7 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...