Explain Codes LogoExplain Codes Logo

How to convert number to words in Java

java
number-to-words
scale-units
decimal-handling
Nikita BarsukovbyNikita Barsukov·Feb 16, 2025
TLDR

A recursive method in Java allows you to create a number-to-words converter. You can tackle each digit set by their place value—units, tens, hundreds—and map them to their word form.

Here's a compact method that works for numbers under 1000:

public class NumberToWords { // Because every programmer likes a good array of words. private static final String[] UNITS = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}; private static final String[] TEENS = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; private static final String[] TENS = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; // Loudly exclaiming the number of hundreds, tens, and units public String numberToWords(int num) { if (num < 10) return UNITS[num]; // Is it a lonely unit? else if (num < 20) return TEENS[num - 10]; // Stepping into the awkward teens else if (num < 100) return TENS[num / 10] + (num % 10 != 0 ? " " + UNITS[num % 10] : ""); // Comfortable with two digits else return UNITS[num / 100] + " Hundred" + (num % 100 != 0 ? " " + numberToWords(num % 100) : ""); // And now, we're talking big numbers! } // Let's see it in action! public static void main(String[] args) { NumberToWords converter = new NumberToWords(); System.out.println(converter.numberToWords(999)); // Output: Nine Hundred Ninety Nine } }

To manage larger numbers, you can use recursion to append the appropriate scale names (e.g., "Thousand", "Million") for every three-digit group.

Bigger Numbers, No Problem!

When dealing with numbers larger than 999, we can extend our previous approach to introduce scale units like "Thousand" and "Million".

private static final String[] SCALE_UNITS = {"", "Thousand", "Million", "Billion", "Trillion"}; private String convertLargeNumbers(int num) { if (num == 0) return "Zero"; // Shout out to our comrade Zero, quietly holding the place. String words = ""; int place = 0; while (num > 0) { if (num % 1000 != 0) { words = numberToWords(num % 1000) + SCALE_UNITS[place] + " " + words; // One. Thousand. Humans. Can't. Count. That. High! } num /= 1000; place++; } return words.trim(); }

Those Decimal Details

Handling decimals and monetary values is a vital part of the conversion process. Here's a method to handle these special cases with style and precision.

public String convertMoney(double amount) { if (amount < 0) return "Negative values? We don't do that here."; // Remember, only positive vibes! int dollars = (int) amount; int cents = (int) ((amount - dollars) * 100); return convertLargeNumbers(dollars) + " Dollars" + (cents > 0 ? " and " + numberToWords(cents) + " Cents" : ""); // Making it rain dollars and cents! }

Speeding Up with Lookup Patterns

Storing common words in arrays enhances performance by providing quick lookup paths. Units, teens, and tens, can be rapidly accessed without the need for complex logic.

// Fast lookup, kind of like cheat codes for programmers. UNITS[3]; // Returns "Three" TEENS[5]; // Returns "Fifteen" TENS[8]; // Returns "Eighty"

Ensuring Accuracy

Test your conversion function with a wide range of numbers. Verifying that scale units are correctly applied at each magnitude of number can prevent semantic errors in word conversion, which are harder to spot.

In the Wild

Built-in functions in database systems like Oracle or libraries like Apache Commons Lang can offer help. If speed and convenience are your desire, they've got your back.