Convert digit amount to word amount

2.How to convert digit amount to word amount ( Best Solutions )

Problem Statement: Convert digit amount to word amount

You need to implement a Java program that converts a given digit amount into its word representation. The program should provide a function convertToWord that takes an integer as input and returns its word equivalent. The program should be able to handle numbers up to the trillion scale.

The program should adhere to the following requirements:

  • The function convertToWord should have the following signature:
public static String convertToWord(int number)
  1. The function should return the word representation of the given number as a string.
  2. The program should handle the special case of the number 0 and return “zero” in such cases.
  3. The program should correctly handle positive numbers and convert them into their word representation, grouping them into thousands, millions, billions, etc.
  4. The program should use the following arrays for conversion:
    • UNITS: An array of word representations for numbers 1 to 19.
    • TENS: An array of word representations for tens numbers (20, 30, 40, …, 90).
    • THOUSANDS: An array of word representations for thousands, millions, billions, etc.
  5. The program should implement the helper function convertThreeDigitNumber to convert a three-digit number into its word representation. This function should have the following signature:
private static String convertThreeDigitNumber(int number)

6. The program should output the word representation of the given number in the main function. An example number 123456789 should be provided, and the corresponding word amount should be printed to the console.

Note: The program does not need to handle negative numbers or decimals. It should only handle whole numbers.

Your task is to implement the above program with the provided requirements and ensure that it correctly converts the digit amount into its word representation.

Solution 1 : Convert digit amount to word amount

To convert a given digit amount into its word equivalent in Java, you can create a function that handles the conversion. Here’s an example implementation:

import java.util.Scanner;
public class Main{
    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", "million", "billion", "trillion"
    };

    public static String convertToWord(int number) {
        if (number == 0) {
            return "zero";
        }

        String word = "";

        int i = 0;
        while (number > 0) {
            if (number % 1000 != 0) {
                word = convertThreeDigitNumber(number % 1000) + THOUSANDS[i] + " " + word;
            }
            number /= 1000;
            i++;
        }

        return word.trim();
    }

    private static String convertThreeDigitNumber(int number) {
        String word = "";

        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) {
            return word;
        }

        return UNITS[number] + " hundred " + word;
    }

    public static void main(String[] args) {
          int number;
          Scanner scanner = new Scanner(System.in);
          //int number = 123456789;
          number = scanner.nextInt();

        
        String wordAmount = convertToWord(number);
        System.out.println(number + " in words: " + wordAmount +" ₹.");
    }
}

In the example above, the convertToWord function takes an integer as input and converts it into its word representation. The function iterates through the number in groups of three digits (thousands, millions, billions, etc.) and converts each group separately using the convertThreeDigitNumber helper function.

The convertThreeDigitNumber function handles the conversion of a three-digit number into its word representation. It uses the UNITS, TENS, and THOUSANDS arrays to store the word equivalents for various numbers.

In the main function, an example number is provided (123456789), and the corresponding word amount is printed to the console.

Running the program will output:

123456789 in words: one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine ₹.

You can modify the main function to test different numbers or integrate the convertToWord function into your existing code as needed.

OR you can input from user by using Scanner

Solution 2 : Convert digit amount to word amount ( Decimal Digit )

Converting decimal digits to word representation can be a complex task due to the different ways decimals can be pronounced in various languages and number systems. If the previous solution does not meet your requirements, you might need to explore external libraries or customize the implementation to suit your specific needs.

One option is to use the Apache Commons Lang library, which provides a utility class called NumberToWords that can handle the conversion of decimal numbers to their word representation. Here’s an example of how you can use it:

Second option is to make some small changes in above code to convert decimal amount to word.

public class Main{
    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", "million", "billion", "trillion"
    };
    public static String convertToWord(int number) {
        if (number == 0) {
            return "zero";
        }
        String word = "";
        int i = 0;
        while (number > 0) {
            if (number % 1000 != 0) {
                word = convertThreeDigitNumber(number % 1000) + THOUSANDS[i] + " " + word;
            }
            number /= 1000;
            i++;
        }
        return word.trim();
    }
    private static String convertThreeDigitNumber(int number) {
        String word = "";
        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) {
            return word;
        }
        return UNITS[number] + " hundred " + word;
    }
    public static void main(String[] args) {
         
         // Scanner scanner = new Scanner(System.in);
           String number = "1239.101";
           String[] arr = number.split("\\.");
           
          //number = scanner.nextInt();
        String amount = convertToWord(Integer.parseInt(arr[0]));
        String decimalAmount = convertToWord(Integer.parseInt(arr[1]));
        System.out.println(number + " in words: " + amount +" point "+decimalAmount +" ₹");
    }
}

Output:

1239.101 in words: onethousand two hundred thirty nine point one hundred one ₹

In the main method, a decimal number in the string format is provided as input. The decimal number is split into two parts, the whole number part, and the decimal part using the split method with a regular expression “\.,” which splits the string at the dot (.) symbol. The convertToWord method is then called for both parts, and the resulting word representations are stored in wordAmount and decimalAmount variables respectively. Finally, the program prints the original number, the word representation of the whole number part, the word “point,” the word representation of the decimal part, and the currency symbol “₹” using the println method.

In the main method, a decimal number in the string format is provided as input. The decimal number is split into two parts, the whole number part, and the decimal part using the split method with a regular expression “\.,” which splits the string at the dot (.) symbol. The convertToWord method is then called for both parts, and the resulting word representations are stored in wordAmount and decimalAmount variables respectively. Finally, the program prints the original number, the word representation of the whole number part, the word “point,” the word representation of the decimal part, and the currency symbol “₹” using the println method.

You can run above code here.

Other Related Assignments:

Ram Chadar

Hello! I'm Ram Chadar, a passionate software developer and freelancer based in Pune. Welcome to my blog, where I share my experiences, insights, and knowledge in the world of software development, different technologies, freelancing, and more.

View all posts by Ram Chadar →

9 thoughts on “2.How to convert digit amount to word amount ( Best Solutions )

  1. It is a pleasure to read this weblog, thanks to its up-to-date information and interesting posts. Look into my web page for some really good points and find out more about Search Engine Optimization.

  2. bosku777 bosku777
    bosku777
    Wonderful blog! I found it while browsing on Yahoo News.
    Do you have any suggestions on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!
    Appreciate it

  3. Howdy! This post couldn’t be written any better!
    Looking at this article reminds me of my previous roommate!
    He always kept preaching about this. I am going to send this post to him.
    Pretty sure he’s going to have a good read. I appreciate you for sharing!

    1. Wow, thank you for your amazing feedback! I’m thrilled to know this article reminded you of your roommate. Sharing it with him will surely bring a smile to his face

Leave a Reply

Your email address will not be published. Required fields are marked *