January 5, 2024

Java Program to Convert Numbers to Words

This post shows how you can write a Java program to convert numbers to words.

For example- If you enter number 123 output should be One hundred twenty three in words. In the post, conversion of number to words is done for both international system and Indian system.

Java program for converting number to words – International system

If you observe in international system comma is placed after every three digits.

223,544,578– Two hundred twenty three million five hundred forty four thousand five hundred seventy eight.

Three digits placed in between each comma are worded in the same way, as shown in the above example 223- Two hundred twenty three, 544- five hundred forty four. You just need to put the correct denomination in between the three digits.

This observation forms the logic for the Java program to convert number to words. Starting from right take 3 digits at a time and convert them to word and place the correct denomination in progression.

public class NumberToWord {
  private static final String[] units = {
    "",
    " one",
    " two",
    " three",
    " four",
    " five",
    " six",
    " seven",
    " eight",
    " nine"
  }; 
  private static final String[] doubles = {
    " ten",
    " eleven",
    " twelve",
    " thirteen",
    " fourteen",
    " fifteen",
    " sixteen",
    " seventeen",
    " eighteen",
    " nineteen"
  };
  private static final String[] tens = {
    "",
    "",
    " twenty",
    " thirty",
    " forty",
    " fifty",
    " sixty",
    " seventy",
    " eighty",
    " ninety"
  };
  private static final String[] hundreds = {
    "",
    " thousand",
    " million",
    " billion"
  };

  private static String convertToWord(int number) {    
    String word = "";    
    int index = 0;
    do {
      // take 3 digits at a time
      int num = number % 1000;
      if (num != 0){
          String str = convertThreeOrLessThanThreeDigitNum(num);
          word = str + hundreds[index] + word;
      }
      index++;
      // move left by 3 digits
      number = number/1000;
    } while (number > 0);
    return word;
  }
  private static String convertThreeOrLessThanThreeDigitNum(int number) {
    String word = "";    
    int num = number % 100;
    // Logic to take word from appropriate array
    if(num < 10){
      word = word + units[num];
    }
    else if(num < 20){
      word = word + doubles[num%10];
    }else{
      word = tens[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("Number-- " + convertToWord(123456789));
    System.out.println("Number-- " + convertToWord(78));
    System.out.println("Number-- " + convertToWord(35658));
    System.out.println("Number-- " + convertToWord(2367904));
    System.out.println("Number-- " + convertToWord(1700));
  }
}
Output
Number--  one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine
Number--  seventy eight
Number--  thirty five thousand six hundred fifty eight
Number--  two million three hundred sixty seven thousand nine hundred four
Number--  one thousand seven hundred

Note that this program works till billion, if you want to go further then add it in “hundreds” array. You will also need to change the type from int to double.

Java program for converting number to words – Indian system

If you observe in Indian system comma is placed after every two digits, barring the first instance where it is placed after three digits.

22,35,44,578– Twenty two crore thirty five lakh forty four thousand five hundred and seventy eight only

In Indian system, starting from right you need to take 3 digits first time and then move by 2 digits in every iteration.

public class NumberToWord {
  private static final String[] units = {
    "",
    " one",
    " two",
    " three",
    " four",
    " five",
    " six",
    " seven",
    " eight",
    " nine"
  }; 
  private static final String[] doubles = {
    " ten",
    " eleven",
    " twelve",
    " thirteen",
    " fourteen",
    " fifteen",
    " sixteen",
    " seventeen",
    " eighteen",
    " nineteen"
  };
  private static final String[] tens = {
    "",
    "",
    " twenty",
    " thirty",
    " forty",
    " fifty",
    " sixty",
    " seventy",
    " eighty",
    " ninety"
  };

  private static final String[] hundreds = {
    "",
    " thousand",
    " lakh",
    " crore",
    " arab",
    " kharab"
  };

  private static String convertToWord(int number) {    
    String num = "";    
    int index = 0;
    int n;
    int digits;
    boolean firstIteration = true;
    do {
      if(firstIteration){
        digits = 1000;
      }else{
        digits = 100;
      }
      n = number % digits;
      if (n != 0){
        String s = convertThreeOrLessThanThreeDigitNum(n);
        num = s + hundreds[index] + num;
      }
      index++;
      number = number/digits;
      firstIteration = false;
    } while (number > 0);
    return num;
  }
  private static String convertThreeOrLessThanThreeDigitNum(int number) {
    String word = "";    
    int num = number % 100;
    // Logic to take word from appropriate array
    if(num < 10){
      word = word + units[num];
    }
    else if(num < 20){
      word = word + doubles[num%10];
    }else{
      word = tens[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("Number-- " + convertToWord(1112345678));
    System.out.println("Number-- " + convertToWord(78));
    System.out.println("Number-- " + convertToWord(35658));
    System.out.println("Number-- " + convertToWord(2367904));
    System.out.println("Number-- " + convertToWord(1700));
  }
}
Output
Number--  one arab eleven crore twenty three lakh forty five thousand six hundred seventy eight
Number--  seventy eight
Number--  thirty five thousand six hundred fifty eight
Number--  twenty three lakh sixty seven thousand nine hundred four
Number--  one thousand seven hundred

That's all for the topic Java Program to Convert Numbers to Words. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment