Conversion of integers to English words Using Javascript.

function amountToWords(numericAmount) {
    const ones = ['Zero', '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 = '';

    if (numericAmount === 0) {
        return 'Zero';
    }

    if (numericAmount < 0) {
        words += 'Negative ';
        numericAmount = Math.abs(numericAmount);
    }

    if (numericAmount >= 1000000) {
        words += amountToWords(Math.floor(numericAmount / 1000000)) + ' Million ';
        numericAmount %= 1000000;
    }

    if (numericAmount >= 1000) {
        words += amountToWords(Math.floor(numericAmount / 1000)) + ' Thousand ';
        numericAmount %= 1000;
    }

    if (numericAmount >= 100) {
        words += amountToWords(Math.floor(numericAmount / 100)) + ' Hundred ';
        numericAmount %= 100;
    }

    if (numericAmount >= 20) {
        words += tens[Math.floor(numericAmount / 10)] + ' ';
        numericAmount %= 10;
    }

    if (numericAmount >= 11 && numericAmount <= 19) {
        words += teens[numericAmount - 11] + ' ';
    } else if (numericAmount > 0) {
        words += ones[numericAmount] + ' ';
    }

    return words.trim();
}

let enWords = amountToWords(122)
console.log (enWords)
// RESULT: One Hundred Twenty Two
Link : https://www.w3resource.com/javascript-exercises/javascript-math-exercise-105.php

The above javascript code helps to convert the integer numbers to English words. This function can be used inside the suitescript to achieve the integer to English word conversion in Netsuite.

Leave a comment

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