Conversion of larger amounts to words using script

To convert large amounts to words,

  • First get the amount from the required field.
  • Use the following function to convert the amount to words.

function capitalizeFirstLetter(string) {
            return string.charAt(0).toUpperCase() + string.slice(1);
        }


        function convertToWords(number) {
            // Array of units for conversion
            const units = ['', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion'];

            // Function to convert a three-digit group into words
            function threeDigitGroupToWords(num) {
                const ones = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
                const teens = ['', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
                const tens = ['', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
                let words = '';
                let hundreds = Math.floor(num / 100);
                let tensUnits = num % 100;
                if (hundreds > 0) {
                    words += ones[hundreds] + ' hundred ';
                }
                if (tensUnits > 0) {
                    if (tensUnits < 10) {
                        words += ones[tensUnits];
                    } else if (tensUnits >= 11 && tensUnits <= 19) {
                        words += teens[tensUnits - 10];
                    } else {
                        let tensDigit = Math.floor(tensUnits / 10);
                        let onesDigit = tensUnits % 10;
                        words += tens[tensDigit];
                        if (onesDigit > 0) {
                            words += '-' + ones[onesDigit];
                        }
                    }
                }
                return words.trim();
            }

            // Function to convert a number into words
            function convertNumberToWords(num) {
                if (num === 0) {
                    return 'zero';
                }
                let words = '';
                let unitIndex = 0;

                while (num > 0) {
                    let threeDigits = num % 1000;
                    if (threeDigits > 0) {
                        words = threeDigitGroupToWords(threeDigits) + ' ' + units[unitIndex] + ' ' + words;
                    }
                    num = Math.floor(num / 1000);
                    unitIndex++;
                }
                return words.trim();
            }
            // Separate dollars and cents
            const dollars = Math.floor(number);
            const cents = Math.round((number - dollars) * 100);
            let words = '';
            // Convert dollars to words
            if (dollars > 0) {
                words += convertNumberToWords(dollars) + ' Rupees';
            }
            // Convert cents to words
            if (cents > 0) {
                words += ' and ' + convertNumberToWords(cents) + ' Paisa';
            }
            return capitalizeFirstLetter(words.trim());
        }

Leave a comment

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