Formatting Numbers as Currency in NetSuite Scripts Using the format Module

n NetSuite SuiteScript, you can format numbers with commas and decimal points across all types of scripts (not just Suitelets) using the format module. The format.formatCurrency() method converts numeric values into a currency format, adding commas for thousands and ensuring two decimal places. This is useful when you need to display or process numbers as currency in any script type, such as Client Scripts, User Event Scripts, Scheduled Scripts, and more.

The core of the code involves the format.format() method. Here, value represents the number, and type: format.Type.CURRENCY ensures the number is formatted as currency.

define([‘N/format’], function(format) {

    function formatCurrencyValue(amount) {

        // Use the formatCurrency method to format the number

        var formattedAmount = format.format({

            value: amount,

            type: format.Type.CURRENCY

        });

        return formattedAmount; // Returns the formatted currency value

    }

    // Example usage in any script (Suitelet, Client Script, etc.)

    var amount = 1000.00;

    var formattedAmount = formatCurrencyValue(amount);

    log.debug(‘Formatted Amount’, formattedAmount); // Logs “1,000.00”

});

Leave a comment

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