Parameters
value |
number | The number to format. Strings are parsed as international numbers first. |
format |
number | string | object | A number of decimal places, a pattern string, or a detailed format object (See Advanced Configuration). |
defaultReturn |
string | Returned when value is not a valid number. If omitted, an error is thrown instead. |
Returns
| string | The number formatted according to the pattern and locale. |
Examples
NL.Locale
loadLibrary("/nl/core/shared/nl.js"); // import NL
loadLibrary("/nl/core/shared/dataTypes.js"); // import NL.Locale
NL.Locale.formatNumber(1234.567, 1); // 1,234.6 (uses default settings from NL.session.locale)
NL.Locale.formatNumber(1234.567, "#,###.0000"); // 1,234.5670 (uses default settings from NL.session.locale)
NL.Locale.formatNumber(
1234.567,
{
pattern: "ยค #,##0.00",
groupSymbol: " ",
decimalSymbol: ",",
symbol: "โฌ",
},
"N/A"
); // โฌ 1 234,57 (uses custom settings)Advanced configuration
For advanced configuration, the format parameter accepts an object with the following keys:
-
pattern, type: string, Unicode number pattern, e.g.ยค#,##0.00โ0/#digits,.decimal,,group,ยคcurrency sign. -
decimalPlaces, type: number, Number of figures kept after the decimal separator. Overrides the pattern. -
significantFigures, type: number, Number of significant figures to keep. Overrides the pattern. -
decimalSymbol, type: string, Decimal separator, substitute for.(usually,or.). -
groupSymbol, type: string, Thousands separator, substitute for,(usually ` ` or,). -
symbol, type: string, Currency symbol, substitute forยค. -
addSymbol, type: boolean, Add the currency symbol if absent from the pattern; placed before/after depending on the currency. -
minusSign, type: string, Prefix used for negative numbers. -
plusSign, type: string, Prefix used for positive numbers.
Going further
Tutorial available at Format prices in Adobe Campaign (NL.Locale cheatsheet).
Unit tests
Tested with acc by running
acc instance exec -f test/integration/nl.locale.spec.js in a sandbox:
loadLibrary("/nl/core/shared/nl.js"); // import NL
loadLibrary("/nl/core/shared/dataTypes.js"); // import NL.Locale
context.test0 = NL.assert(NL.Locale.getLocale().locale === NL.session.locale.locale);
NL.session.setLocale("en-us");
context.test1 = NL.assert(NL.Locale.formatNumber(1234.567) === "1,235");
context.test2 = NL.assert(NL.Locale.formatNumber(1234.567, 2) === "1,234.57");
NL.session.setLocale("fr-fr");
context.test3 = NL.assert(NL.Locale.formatNumber(1234.567) === "1 235");
context.test4 = NL.assert(NL.Locale.formatNumber(1234.567, 2) === "1 234,57");
context.test8 = NL.assert(
NL.Locale.formatNumber(1234.567, {
pattern: "ยค #,##0.00",
groupSymbol: " ",
decimalSymbol: ",",
symbol: "โฌ",
}) === "โฌ1 234,57"
);
context.test11 = NL.assert(NL.Locale.getLocale("en-us").settings.currencySymbol === "$");
context.test12 = NL.assert(NL.Locale.getLocale("en-gb").settings.currencySymbol === "ยฃ");
context.test13 = NL.assert(NL.Locale.getLocale("fr").settings.currencySymbol === "โฌ"); // only fr-fr and fr-be exist but the framework gets the first 2 letters
context.test14 = NL.assert(NL.Locale.getLocale("ja").settings.shortDate === "%4Y/%2M/%2D");
context.test15 = NL.assert(NL.Locale.getLocale("zh").settings.decimalSymbol === ".");
context.test16 = NL.assert(NL.Locale.getLocale("ko").settings.digitGroupingSymbol === ",");
context.test20 = NL.assert(
NL.Locale.getLocale("xxx").settings === NL.Locale.getLocale(NL.Locale.defaultLanguage).settings
);See also
-
๐ queryDef- Helper to run a SQL SELECT query with JSON and returns an XML element. -
๐ DOMElement- Adobe Campaign's XML element: the type returned by queryDef and SOAP calls. Can be read and traversed. -
๐ NL.is- Adobe Campaign's type helpers to check XML values.