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 :
Input | Output |
123 | One Hundred Twenty Three |
1234 | One Thousand Two Hundred Thirty Four |
12345 | Twelve Thousand Three Hundred Forty Five |
123456 | One Hundred Twenty Three Thousand Four Hundred Fifty Six |
1234567 | One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven |
1234568 | One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Eight |
12345670 | Twelve Million Three Hundred Forty Five Thousand Six Hundred Seventy |
123456709 | One Hundred Twenty Three Million Four Hundred Fifty Six Thousand Seven Hundred Nine |
1234567090 | One Billion Two Hundred Thirty Four Million Five Hundred Sixty-Seven Thousand Ninety |
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 = "";
break;
case 1:
tradBillions = convertUptoThousand(billions) + " Billion ";
break;
default:
tradBillions = convertUptoThousand(billions) + " Billion ";
}
String result = tradBillions;
String tradMillions;
switch (millions) {
case 0:
tradMillions = "";
break;
case 1:
tradMillions = convertUptoThousand(millions) + " Million ";
break;
default:
tradMillions = convertUptoThousand(millions) + " Million ";
}
result = result + tradMillions;
String tradHundredThousands;
switch (hundredThousands) {
case 0:
tradHundredThousands = "";
break;
case 1:
tradHundredThousands = "One Thousand ";
break;
default:
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
System.out.println(convertNumberToWord(5));
System.out.println(convertNumberToWord(89));
System.out.println(convertNumberToWord(656));
System.out.println(convertNumberToWord(1301));
System.out.println(convertNumberToWord(13512));
System.out.println(convertNumberToWord(567319));
System.out.println(convertNumberToWord(90908890));
System.out.println(convertNumberToWord(2000000000));
System.out.println(convertNumberToWord(569999999));
System.out.println(convertNumberToWord(3233000000L));
System.out.println(convertNumberToWord(5000000));
System.out.println(convertNumberToWord(333333333));
System.out.println(convertNumberToWord(5000400));
System.out.println(convertNumberToWord(600000));
System.out.println(convertNumberToWord(4000000));
}
}
Output
Five
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 !!!
You must be logged in to post a comment.