Format prices, dates, and other locale-specific values in Adobe Campaign emails and workflows β for example, to display currency amounts that match your audienceβs regional conventions or parse user input according to locale rules.
The built-in NL.Locale library provides formatting and parsing functions that leverage the session locale settings, while Format offers simpler number formatting for basic use cases.
Preparation
Load the required libraries in your JavaScript activity or JSSP:
loadLibrary("/nl/core/shared/nl.js"); // import NL
loadLibrary("/nl/core/shared/dataTypes.js"); // import NL.Locale
Walkthrough
Format numbers using NL.Locale:
var x = 1234567.89;
NL.Locale.formatNumber(x); // 1,234,568 (uses default settings from NL.session.locale)
NL.Locale.formatNumber(x, { addSymbol: true }); // $1,234,568
var formatUS = {
pattern: "Β€#,##0.00",
groupSymbol: ",",
decimalSymbol: ".",
symbol: "$",
};
NL.Locale.formatNumber(x, formatUS); // $1,234,567.89
var formatFR = {
pattern: "#,##0.00 Β€",
groupSymbol: " ",
decimalSymbol: ",",
symbol: "β¬",
};
NL.Locale.formatNumber(x, formatFR); // 1 234 567,89 β¬
// Simpler formatting with Format
Format.formatNumber(x); // 1,234,567.89Reference
Find all settings in the π NL.Locale reference.
All other NL functions and vars are available in the π AC JS Reference.
Validation & troubleshooting
If formatting returns unexpected results, verify logInfo(NL.session.locale) contains the correct regional settings for your target audience.
Going further
Prepare the number by casting it to a number if itβs an XML or string:
/**
* Format a price to always have 2 decimals
* @example prepareNumber("12.3") = "12.30"
*
* @param {xml|string|int|float} p the number to cast
* @return {number} the casted number
*/
function prepareNumber(p) {
if (typeof p == "xml") {
p = p.toString();
}
if (typeof p == "string") {
p = parseFloat(p);
}
return p;
}