N/format/i18n Module Script Samples

The following script samples demonstrate how to use the features of the N/format/i18n module:

Format 12345 as a German String

The following sample spells out the number 12345 as a string in German, “zwölf­tausend­drei­hundert­fünf­und­vierzig”.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

            /**
 * @NApiVersion 2.x
 */
require(['N/format/i18n'], function(format) {
     var spellOut = format.spellOut({
        number: 12345, 
        locale: "DE"
    });
    // spellOut is 'zwölf­tausend­drei­hundert­fünf­und­vierzig'
}); 

          

Format a Number as a String Using N/format/i18n

The following sample formats a number as a string.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

            /**
 * @NApiVersion 2.x
 */
require(['N/format/i18n'], function(format) {
    var numberFormatter = format.getNumberFormatter();
        
    var gs = numberFormatter.groupSeparator;         // gs is ','
    var ds = numberFormatter.decimalSeparator;       // ds is '.'
    var precision = numberFormatter.precision;       // precision is '2'
    var nnf = numberFormatter.negativeNumberFormat;  // nnf is 'BRACKETS'
 
    var formatNum1 = numberFormatter.format({
        number: 12.53
    }); // formatNum1 is '12.53'
    
    var formatNum2 = numberFormatter.format({
        number: 12845.22
    }); // formatNum2 is '12,845.22'
    
    var formatNum3 = numberFormatter.format({
        number: -5421
    }); // formatNum3 is '(5,421.00)'
    
    var formatNum4 = numberFormatter.format({
        number: 0.00
    }); // formatNum4 is '0.00'
    
    var formatNum5 = numberFormatter.format({
        number: 0.3456789
    }); // formatNum5 is '0.35'
}); 

          

Format Numbers as Currency Strings

The following sample formats numbers as currency strings.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

            /**
 * @NApiVersion 2.x
 */
require(['N/format/i18n'], function(format) {
    var curFormatter = format.getCurrencyFormatter({currency: "EUR"});

    var curCur = curFormatter.currency;                  // curCur is 'EUR'
    var numberFormat = curFormatter.numberFormatter;

    var curSymbol = curFormatter.symbol;                 // curSymbol is '€'
    var curSeparator = numberFormat.groupSeparator;      // curSeparator is ' '
    var curDecimalSep = numberFormat.decimalSeparator;   // curDecimalSep is ','
    var curPrecision = numberFormat.precision;           // curPrecision is 2
    var curNNF = numberFormat.negativeNumberFormat;      // curNNF is MINUS

    curFormatNum1 = curFormatter.format({
        number: 12.53
    }); // curFormat1 is '€12,53'

    curFormatNum2 = curFormatter.format({
        number: -5421
    }); // curFormat2 is '€-5 421,00'

    curFormatNum3 = curFormatter.format({
        number: 0.00
    }); // curFormat3 is '€0,00'

    curFormatNum4 = curFormatter.format({
        number: 0.3456789
    }); // curFormat4 is '€0,35'
}); 

          

Parse a Czech Phone Number

The following sample parses a Czech phone number and logs the resulting country code, extension, national number, number of leading zeros, carrier code, and raw input.

Note:

This sample script uses the require function so you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (a script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

            /**
 * @NApiVersion 2.x
 */
require(['N/format/i18n'], function(format) {
    var origNumberStr = "602547854ext.154";
    var pnParser = format.getPhoneNumberParser({
        defaultCountry: format.Country.CZECH_REPUBLIC
    });
    var phoneNumber = pnParser.parse({
        number: origNumberStr
    });

    /* after parsing:
       phoneNumber.countryCode is '420'
       phoneNumber.extension is '154'
       phoneNumber.nationalNumber is '602547854'
       phoneNumber.numberofLeadingZeros is 1
       phoneNumber.carrierCode is ' '
       phoneNumber.rawInput is '602547854ext.154'
    */

    var pnFormatter = format.getPhoneNumberFormatter({});
    var strNumber = pnFormatter.format({
        number: phoneNumber
    });  // strNumber is '+420 602 547 854 ext. 154'
}); 

          

Parse a U.S. Phone Number

The following sample parses a U.S. phone number and logs the country code, extentions, national number, number of leading zerios, carrier code, and raw input.

Note:

This sample script uses the require function so you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (a script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

            /**
 * @NApiVersion 2.x
 */
require(['N/format/i18n'], function(format) {
    var origNumberStr = "7524105210ext.154";
    var pnParser = format.getPhoneNumberParser({
        defaultCountry: format.Country.UNITED_STATES
    });
    var phoneNumber = pnParser.parse({
        number: origNumberStr
    });

    /* after parsing:
       phoneNumber.countryCode is '1'
       phoneNumber.extension is '154'
       phoneNumber.nationalNumber is '7524105210'
       phoneNumber.numberofLeadingZeros is 1
       phoneNumber.carrierCode is ' '
       phoneNumber.rawInput is '7524105210ext.154'
    */

    var pnFormatter = format.getPhoneNumberFormatter({
        formatType: format.PhoneNumberFormatType.NATIONAL
    });
    var strNumber = pnFormatter.format({
        number: phoneNumber
    }); // strNumber is '(752) 410-5210 ext. 154'
}); 

          

Format Numbers Based on the Locale Parameter

The following sample formats numbers based on the locale parameter.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

            /**
 * @NApiVersion 2.x
 */
require(['N/format/i18n'], function(format) {
    var numberFormatter = format.getNumberFormatter({
        locale: "fr_FR"
    });

    var gs = numberFormatter.groupSeparator;        // gs is ' '
    var ds = numberFormatter.decimalSeparator;      // ds is ','
    var prec = numberFormatter.precision;           // prec is 2
    var nnf = numberFormatter.negativeNumberFormat; // nnf is MINUS

    var number1 = numberFormatter.format({
        number: 123456.55
    });  // number1 is '123 456,55'

    var number2 = numberFormatter.format({
        number: -123456.55
    });  // number2 is '-123 456,55'
}); 

          

Format Currency Based on the Locale Parameter

The following sample formats currency based on the locale parameter.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

            /**
 * @NApiVersion 2.x
 */
require(['N/format/i18n'], function(format) {
    var curFormatter = format.getCurrencyFormatter({
        locale: "IT_IT"
    });

    var curCur = curFormatter.currency;   // curCur is 'EUR'
    var sym = curFormatter.symbol;        // sym is '€'
    
    var numberFormat = curFormatter.numberFormatter;

    var gs = numberFormat.groupSeparator;         // gs is '.'
    var ds = numberFormat.decimalSeparator;       // ds is ','
    var prec = numberFormat.precision;            // prec is 2
    var nnf = numberFormat.negativeNumberFormat;  // nnf is MINUS

    var currency1 = curFormatter.format({
        number: 123456.55
    });// currency1 is '€123.456,55'

    var currency2 = curFormatter.format({
        number: -123456.55
    });  // currency2 is '€-123.456,55'
}); 

          

Format Numbers and Currencies Based on the English-India Locale Parameter

The following sample formats numbers and currencies based on the English-India (en_IN) locale parameter.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

            /**
 * @NApiVersion 2.x
 */
require(['N/format/i18n'], function(format) {
    var curFormatter = format.getCurrencyFormatter({
        locale: "en_IN"
    });

    var curCur = curFormatter.currency;  // curCur is 'INR'

    var numberFormat = curFormatter.numberFormatter;
    var sym = curFormatter.symbol;                  // sym is '₹'
    var gs = numberFormat.groupSeparator;           // gs is ','
    var ds = numberFormat.decimalSeparator;         // ds is '.'
    var prec = numberFormat.precision;              // prec is 2
    var nnf = numberFormat.negativeNumberFormat;    // nnf is MINUS

    var currency1 = curFormatter.format({
        number: 12345678.55
    });  // currency1 is '₹1,23,45,678.55'

    var currency2 = curFormatter.format({
        number: -345678.55
    });  // currency2 is '₹-3,45,678.55'
}); 

          

General Notices