Introduction
In programming, converting numbers into their word representation is a common problem. This can be useful in many applications such as invoices, reports, or writing checks. In this tutorial, we will demonstrate how to write a simple Java program that converts a number into words.
Objective
The objective of this program is to convert an integer (from 0 to 999,999,999) into its word equivalent. For example, the number “123” will be converted to “one hundred and twenty-three” in words.
Java Code to Convert Number to Words
import java.text.DecimalFormat; public class NumberToWordsConverter { private static final String[] belowTwenty = { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "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[] thousands = { "", "Thousand", "Million" }; public static void main(String[] args) { long number = 123456789; System.out.println("The number in words: " + convertToWords(number)); } public static String convertToWords(long num) { if (num == 0) return belowTwenty[0]; StringBuilder words = new StringBuilder(); int thousandIndex = 0; while (num > 0) { if (num % 1000 != 0) { StringBuilder temp = new StringBuilder(); convertHundreds((int) (num % 1000), temp); temp.append(" ").append(thousands[thousandIndex]); words.insert(0, temp.toString() + " "); } num /= 1000; thousandIndex++; } return words.toString().trim(); } private static void convertHundreds(int num, StringBuilder temp) { if (num > 99) { temp.append(belowTwenty[num / 100]).append(" Hundred "); num %= 100; } if (num > 19) { temp.append(tens[num / 10]).append(" "); num %= 10; } if (num > 0) { temp.append(belowTwenty[num]).append(" "); } } }
Explanation of the Program
This program defines a class named NumberToWordsConverter
which includes the logic to convert a given number into words. It does so by breaking the number down into chunks of three digits (thousands, hundreds, etc.), and converting each chunk into its word representation. The program uses three arrays:
- belowTwenty – This array contains the word representation of numbers from 0 to 19.
- tens – This array contains the word representation of tens multiples from 20 to 90.
- thousands – This array holds the names for large number categories like “Thousand”, “Million”, etc.
Program Flow
1. The convertToWords
method is the main method that accepts a number and converts it to words. It iterates through the number, chunking it into groups of three digits (hundreds, thousands, etc.).
2. The convertHundreds
method handles the conversion of a number less than 1000 into words by calling the belowTwenty
and tens
arrays.
3. The result is then returned as a string.
How to Run the Program
- Open your Java development environment (IDE) or any text editor.
- Copy and paste the code into a new Java file (e.g.,
NumberToWordsConverter.java
). - Compile the program using the command
javac NumberToWordsConverter.java
. - Run the program using the command
java NumberToWordsConverter
. - The output will display the word representation of the given number.