Monday, December 27, 2021

Convert Numbers to Words Java Program

In this post we’ll see a Java program to convert numbers to words. For example, in the Java program if number 5467 is passed then the output in words should be- Five thousand four hundred sixty seven.

Number systems

In Indian system numbers are called differently and even placement is different than in the international system.

For example-

11,23,45,657- Eleven crore twenty three lakhs forty five thousand six hundred fifty seven

As you can see the numbers here are Crore, Lakhs also the digits are grouped in twos except the hundreds where three digits are grouped.

In international system same number will be written as-

112,345,657- One hundred twelve million three hundred forty five thousand six hundred fifty seven

Here you can notice that the digits are grouped in threes and the digit group separator (hundred, thousand, million...) is placed after every three digits.

We’ll have Java program for converting numbers to words for both Indian and international system and how digits are grouped forms the logic for the programs.

Converting number to words in Java – International System

As already stated above, in the program we’ll have to get the three digits of the number in each iteration and put the correct place value after those three digits.

In the program we’ll also have different arrays for the numbers.

public class ConvertNumToWord {
  private static final String[] units = {
    "",
    " one",
    " two",
    " three",
    " four",
    " five",
    " six",
    " seven",
    " eight",
    " nine"
  }; 
  private static final String[] twoDigits = {
    " ten",
    " eleven",
    " twelve",
    " thirteen",
    " fourteen",
    " fifteen",
    " sixteen",
    " seventeen",
    " eighteen",
    " nineteen"
  };
  private static final String[] tenMultiples = {
    "",
    "",
    " twenty",
    " thirty",
    " forty",
    " fifty",
    " sixty",
    " seventy",
    " eighty",
    " ninety"
  };
  private static final String[] placeValues = {
    " ",
    " thousand",
    " million",
    " billion",
    " trillion"
  };
        
  private static String convertNumber(long number) {    
    String word = "";    
    int index = 0;
    do {
      // take 3 digits in each iteration
      int num = (int)(number % 1000);
      if (num != 0){
        String str = ConversionForUptoThreeDigits(num);
        word = str + placeValues[index] + word;
      }
      index++;
      // next 3 digits
      number = number/1000;
    } while (number > 0);
    return word;
  }
    
  private static String ConversionForUptoThreeDigits(int number) {
    String word = "";    
    int num = number % 100;
    if(num < 10){
      word = word + units[num];
    }
    else if(num < 20){
      word = word + twoDigits[num%10];
    }else{
      word = tenMultiples[num/10] + units[num%10];
    }
    
    word = (number/100 > 0)? units[number/100] + " hundred" + word : word;
    return word;
  }
    
  public static void main(String[] args) {
    System.out.println("1234123456789- " + convertNumber(1234123456789L));
    System.out.println("123456789- " + convertNumber(123456789));
    System.out.println("37565820- " + convertNumber(37565820));
    System.out.println("9341947- " + convertNumber(9341947));
    System.out.println("37000- " + convertNumber(37000));
    System.out.println("1387- " + convertNumber(1387));
    System.out.println("10- " + convertNumber(10));
    System.out.println("41- " + convertNumber(41));
  }
}

Output

1234123456789-  one trillion two hundred thirty four billion one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine 
123456789-  one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine 
37565820-  thirty seven million five hundred sixty five thousand eight hundred twenty 
9341947-  nine million three hundred forty one thousand nine hundred forty seven 
37000-  thirty seven thousand
1387-  one thousand three hundred eighty seven 
10-  ten 
41-  forty one 

Converting number to words in Java – Indian System

As already stated above, for Indian system in the program we’ll have to get the three digits of the number in first iteration and then two digits in each iteration and put the correct place value after each iteration.

The array for placeValues will change as per Indian system.

public class ConvertNumToWord {
  private static final String[] units = {
    "",
    " one",
    " two",
    " three",
    " four",
    " five",
    " six",
    " seven",
    " eight",
    " nine"
  }; 
  private static final String[] twoDigits = {
    " ten",
    " eleven",
    " twelve",
    " thirteen",
    " fourteen",
    " fifteen",
    " sixteen",
    " seventeen",
    " eighteen",
    " nineteen"
  };
  private static final String[] tenMultiples = {
    "",
    "",
    " twenty",
    " thirty",
    " forty",
    " fifty",
    " sixty",
    " seventy",
    " eighty",
    " ninety"
  };
  private static final String[] placeValues = {
    "",
    " thousand",
    " lakh",
    " crore",
    " arab",
    " kharab"
  };
        
  private static String convertNumber(long number) {    
    String word = "";    
    int index = 0;
    boolean firstIteration = true;
    int divisor;
    do {
      divisor = firstIteration ? 1000 : 100;
      // take 3 or 2 digits based on iteration
      int num = (int)(number % divisor);          
      if (num != 0){
        String str = ConversionForUptoThreeDigits(num);
        word = str + placeValues[index] + word;
      }
      index++;
      // next batch of digits
      number = number/divisor;
      firstIteration = false;
    } while (number > 0);
    return word;
  }
    
  private static String ConversionForUptoThreeDigits(int number) {
    String word = "";    
    int num = number % 100;
    if(num < 10){
      word = word + units[num];
    }
    else if(num < 20){
      word = word + twoDigits[num%10];
    }else{
      word = tenMultiples[num/10] + units[num%10];
    }
    
    word = (number/100 > 0)? units[number/100] + " hundred" + word : word;
    return word;
  }
    
  public static void main(String[] args) {
    System.out.println("1234123456789- " + convertNumber(1234123456789L));
    System.out.println("326776756- " + convertNumber(326776756));
    System.out.println("37565820- " + convertNumber(37565820));
    System.out.println("9341947- " + convertNumber(9341947));
    System.out.println("37000- " + convertNumber(37000));
    System.out.println("1387- " + convertNumber(1387));
    System.out.println("10- " + convertNumber(10));
    System.out.println("5- " + convertNumber(5));
  }
}

Output

1234123456789-  twelve kharab thirty four arab twelve crore thirty four lakh fifty six thousand seven hundred eighty nine
326776756-  thirty two crore sixty seven lakh seventy six thousand seven hundred fifty six
37565820-  three crore seventy five lakh sixty five thousand eight hundred twenty
9341947-  ninety three lakh forty one thousand nine hundred forty seven
37000-  thirty seven thousand
1387-  one thousand three hundred eighty seven
10-  ten
5-  five

That's all for this topic Convert Numbers to Words Java Program. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page


Related Topics

  1. Convert double to int in Java
  2. Arrange Non-Negative Integers to Form Largest Number - Java Program
  3. Split a String Java Program
  4. Converting String to int in Java
  5. Converting String to Enum Type in Java

You may also like-

  1. Printing Numbers in Sequence Using Threads Java Program
  2. How to Create PDF in Java Using OpenPDF
  3. Difference Between Two Dates in Java
  4. Creating Tar File And GZipping Multiple Files in Java
  5. How to Pass Command Line Arguments in Eclipse
  6. static Import in Java With Examples
  7. Java String Interview Questions And Answers
  8. Spring MVC Pagination Example Using PagedListHolder

No comments:

Post a Comment