Java - Converting Amount to Words

Many programs, particularly those related to billing or finance, may require you to translate a numerical quantity into Indian rupees. For instance, an amount like ₹1,250 should be written as "One Thousand Two Hundred Fifty Rupees." This blog post will guide you on how to convert an amount in Indian Rupees to its word equivalent using Java.

1. Understanding the Indian Number System

In India, the number system differs slightly from the Western system. Instead of "thousand," "million," etc., it uses terms like "lakh" and "crore":

1 Lakh = 100,000

1 Crore = 10,000,000

So, for example:

₹1,00,000 is read as "One Lakh"

₹1,00,00,000 is read as "One Crore"

Program:

import java.text.DecimalFormat;

public class Amount_To_Words_Converter {

    private static final String[] units = {

        "", "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", "Lakh", "Crore"};

    // Convert numbers into words (integer part)

    public static String convertNumberToWords(int number) {

        if (number == 0) {

            return "Zero";

        }

        String words = "";

        int thousandCounter = 0;

        // Process number in groups of digits (hundreds, thousands, lakhs, crores)

        while (number > 0) {

            int remainder = number % 100;

            if (thousandCounter == 1) {

                remainder = number % 100; // For Lakh, we take last 2 digits

            } else if (thousandCounter > 1) {

                remainder = number % 100; // For Crore, take last 2 digits

            } else {

                remainder = number % 1000; // For Thousand, take last 3 digits

            }

            if (remainder != 0) {

                words = convertThreeDigits(remainder, thousandCounter) + words;

            }

            number /= (thousandCounter == 1) ? 100 : 1000; // Divide for Lakh and Crore

            thousandCounter++;

        }

        return words.trim();

    }

    // Convert numbers less than 1000 into words

    private static String convertThreeDigits(int number, int thousandCounter) {

        String word = "";

        if (thousandCounter == 1) { // Lakh

            word = units[number % 100] + " ";

            number /= 100;

            word = tens[number] + " " + word;

        } else if (thousandCounter > 1) { // Crore

            word = units[number % 100] + " ";

            number /= 100;

            word = tens[number] + " " + word;

        } else { // Thousand

            if (number % 100 < 20) {

                word = units[number % 100];

                number /= 100;

            } else {

                word = units[number % 10];

                number /= 10;

                word = tens[number % 10] + " " + word;

                number /= 10;

            }

            if (number > 0) {

                word = units[number] + " Hundred " + word;

            }

        }

        return word.trim() + (thousandCounter == 0 ? " " : " " + thousands[thousandCounter] + " ");

    }

    // Convert decimal part into words (for paise)

    public static String convertDecimalToWords(double amount) {

        DecimalFormat df = new DecimalFormat("#.##");

        String formattedAmount = df.format(amount);

        String[] parts = formattedAmount.split("\\.");

        // Convert the integer part to words

        int integerPart = Integer.parseInt(parts[0]);

        String integerPartInWords = convertNumberToWords(integerPart);

        // Convert the decimal part to words if exists

        if (parts.length > 1) {

            int decimalPart = Integer.parseInt(parts[1]);

            if (decimalPart != 0) {

                String decimalPartInWords = convertNumberToWords(decimalPart);

                return integerPartInWords + " Rupees and " + decimalPartInWords + " Paise";

            }

        }

        return integerPartInWords + " Rupees";

    }

    public static void main(String[] args) {

        double amount = 123456.78;

        // Convert and print the amount in words

        String amountInWords = convertDecimalToWords(amount);

        System.out.println("Amount in Words: " + amountInWords);

    }

}