Header Ads Widget

Responsive Advertisement

Currency Number to Words in Java Using Recursive nlog2n Approach

  • Converting a number to words in Java can be quite complex, especially if you need to handle large numbers or different formats. Here's an implementation that can convert numbers up to billions into words. This example handles English language conversion.

    Example Code

    java

    public class NumberToWords {

     

        private static final String[] BELOW_TWENTY = {

            "", "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"

        };

     

        public static void main(String[] args) {

            System.out.println(numberToWords(123));         // One Hundred Twenty Three

            System.out.println(numberToWords(12345));       // Twelve Thousand Three Hundred Forty Five

            System.out.println(numberToWords(1234567));     // One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven

            System.out.println(numberToWords(1234567891));  // One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One

        }

     

        public static String numberToWords(int num) {

            if (num == 0) return "Zero";

     

            int i = 0;

            String words = "";

     

            while (num > 0) {

                if (num % 1000 != 0) {

                    words = helper(num % 1000) + THOUSANDS[i] + " " + words;

                }

                num /= 1000;

                i++;

            }

     

            return words.trim();

        }

     

        private static String helper(int num) {

            if (num == 0)

                return "";

            else if (num < 20)

                return BELOW_TWENTY[num] + " ";

            else if (num < 100)

                return TENS[num / 10] + " " + helper(num % 10);

            else

                return BELOW_TWENTY[num / 100] + " Hundred " + helper(num % 100);

        }

    }

     

    Explanation

    1. Constants Arrays:
      • BELOW_TWENTY: Contains words for numbers from 0 to 19.
      • TENS: Contains words for multiples of ten from 20 to 90.
      • THOUSANDS: Contains the units for thousands, millions, and billions.
    1. numberToWords Method:
      • This is the main method that converts an integer to words.
      • It checks if the number is zero and returns "Zero".
      • It iterates through the number in chunks of three digits (thousands) and builds the word representation by calling the helper method.
      • It keeps track of the thousands place using the THOUSANDS array.
    1. helper Method:
      • This method handles numbers less than 1000 and converts them to words.
      • It recursively processes the number, handling hundreds, tens, and units separately.

    Example Usage

    Ø  numberToWords(123) returns "One Hundred Twenty Three".

    Ø  numberToWords(12345) returns "Twelve Thousand Three Hundred Forty Five".

    Ø  numberToWords(1234567) returns "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven".

    Ø  numberToWords(1234567891) returns "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One".


    Number to Word convert of currency
    Number to Word convert of currency







Post a Comment

0 Comments