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'
});
Related Topics
- N/format/i18n Module Script Samples
- Format 12345 as a German String
- Format Numbers as Currency Strings
- Parse a Czech Phone Number
- Parse a U.S. Phone Number
- Format Numbers Based on the Locale Parameter
- Format Currency Based on the Locale Parameter
- Format Numbers and Currencies Based on the English-India Locale Parameter
- SuiteScript 2.x Modules
- SuiteScript 2.x