Write a program to convert a non-negative integer number to its English words representation

In this program converting Non-negative numbers to English words through Java Program. You can use the same logic to implement with other languages like C, C++, C#, Python, etc.

This program is most frequently asked in programming label tests or interviews to check your logical skills. You can also utilize the same code while generating invoices, bills, reports where want to show sum, average, gross total etc. in form of words.

For Example :

123One Hundred Twenty Three
1234One Thousand Two Hundred Thirty Four
12345Twelve Thousand Three Hundred Forty Five
123456One Hundred Twenty Three Thousand Four Hundred Fifty Six
1234567One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven
1234568One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Eight
12345670Twelve Million Three Hundred Forty Five Thousand Six Hundred Seventy
123456709One Hundred Twenty Three Million Four Hundred Fifty Six Thousand Seven Hundred Nine
1234567090One Billion Two Hundred Thirty Four Million Five Hundred Sixty-Seven Thousand Ninety
Program to convert numbers to English Words

Java Program

To convert a number to English word core logic is to check 10th position of the number the add words for the same. Based on the range between billion to thousand then take reminder and add word (thousand, million or billion) etc. then further dividend of the number by the range and further pass for convert as long as not reaching to less than thousand. Finally, when numbers reach to between 1-20 take the words from map 1-20 range.

Source Code:

package programming;

import java.text.DecimalFormat;

public class NNNumberToWordExample {
	// array of string type for one digit numbers
	private static final String[] doubleDigits = { "", " Ten", " Twenty", " Thirty", " Forty", " Fifty", " Sixty",
			" Seventy", " Eighty", " Ninety" };
	// array of string for two digits numbers
	private static final String[] singleDigit = { "", " One", " Two", " Three", " Four", " Five", " Six", " Seven",
			" Eight", " Nine", " Ten", " Eleven", " Twelve", " Thirteen", " Fourteen", " Fifteen", " Sixteen",
			" Seventeen", " Eighteen", " Nineteen" };

	// converts a number to words (up to 1000)
	private static String convertUptoThousand(int number) {
		String soFar;
		if (number % 100 < 20) {
			soFar = singleDigit[number % 100];
			number = number / 100;
		} else {
			soFar = singleDigit[number % 10];
			number = number / 10;
			soFar = doubleDigits[number % 10] + soFar;
			number = number / 10;
		if (number == 0)
			return soFar;
		return singleDigit[number] + " Hundred " + soFar;

	// converts a long number (0 to 999999999) to string
	public static String convertNumberToWord(long number) {
		// checks whether the number is zero or not if number is zero return zero
		if (number == 0) {
			return "zero";
		// convert long value to string
		String num = Long.toString(number);
		// for creating a mask padding with "0"
		String pattern = "000000000000";
		 * Convert to DecimalFormat using the specified pattern and also provides the
		 * symbols to default locale
		DecimalFormat decimalFormat = new DecimalFormat(pattern);
		// format a number of the DecimalFormat
		num = decimalFormat.format(number);
		 * format: XXXnnnnnnnnn the subString() method returns a new string that is a
		 * substring of this string the substring begins at the specified beginIndex and
		 * extends to the character at index endIndex - 1 the parseInt() method converts
		 * the string into integer
		int billions = Integer.parseInt(num.substring(0, 3));
		// format to: nnnXXXnnnnnn
		int millions = Integer.parseInt(num.substring(3, 6));
		// format to: nnnnnnXXXnnn
		int hundredThousands = Integer.parseInt(num.substring(6, 9));
		// format to: nnnnnnnnnXXX
		int thousands = Integer.parseInt(num.substring(9, 12));

		String tradBillions;

		switch (billions) {
		case 0:
			tradBillions = "";
		case 1:
			tradBillions = convertUptoThousand(billions) + " Billion ";
			tradBillions = convertUptoThousand(billions) + " Billion ";

		String result = tradBillions;
		String tradMillions;
		switch (millions) {
		case 0:
			tradMillions = "";
		case 1:
			tradMillions = convertUptoThousand(millions) + " Million ";
			tradMillions = convertUptoThousand(millions) + " Million ";
		result = result + tradMillions;

		String tradHundredThousands;
		switch (hundredThousands) {
		case 0:
			tradHundredThousands = "";
		case 1:
			tradHundredThousands = "One Thousand ";
			tradHundredThousands = convertUptoThousand(hundredThousands) + " Thousand ";
		result = result + tradHundredThousands;

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

		// removing extra space if any
		return result.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " ");

	public static void main(String args[]) {
		// Test cases to convert number to words


Eighty Nine
Six Hundred Fifty Six
One Thousand Three Hundred One
Thirteen Thousand Five Hundred Twelve
Five Hundred Sixty Seven Thousand Three Hundred Nineteen
Ninety Million Nine Hundred Eight Thousand Eight Hundred Ninety
Two Billion 
Five Hundred Sixty Nine Million Nine Hundred Ninety Nine Thousand Nine Hundred Ninety Nine
Three Billion Two Hundred Thirty Three Million 
Five Million 
Three Hundred Thirty Three Million Three Hundred Thirty Three Thousand Three Hundred Thirty Three
Five Million Four Hundred 
Six Hundred Thousand 
Four Million 

Hope this program help you and clear your logics. Please share in comments.

Happy Learning !!!

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s