Format Numbers with AMPscript
How AMPscript number formatting changes decimal and grouping separators, when locale-aware output suits your email, and where plain unformatted numbers work better.
Formatting numbers in AMPscript, the scripting language used in Salesforce Marketing Cloud emails, landing pages and messages, turns a raw value such as 1234567.891 into a string that matches what a reader expects, such as 1,234,567.89 for a US audience or 1.234.567,89 for a German one. This review explains what that conversion changes, when it is the right choice, and where it can cause problems for copying, parsing or accessibility. No product-specific test results or measurements were supplied for this article, so the behavior described here comes from general, documented principles of AMPscript and locale formatting. Verify it with a preview or test send in your own account.
Is locale-aware number formatting suitable for your use?
For numbers that people will read, such as prices, balances, points totals and quantities, formatting is usually worth doing. A correctly grouped and localized number is faster to read, and it avoids the serious misreading that happens when a period means a decimal point to one reader and a thousands separator to another.
The main tradeoff is that formatting produces text, not a number. Once a value is formatted, it is no longer safe to do arithmetic on it, compare it, or store it back into a numeric field. Any downstream system may also misinterpret it if that system assumes a different locale.
Plain, unformatted numbers are the better choice in these cases:
- Values written to data extensions, tracking parameters, or URLs that another system will parse.
- IDs, account numbers, postal codes, and order numbers, where grouping separators would corrupt the value.
- Any value used in later AMPscript calculations or comparisons.
- Exports and integrations where the receiving system's locale is unknown.
A workable rule is to keep the raw numeric value in data and logic, and format it only at the moment it is displayed to a person.
What formatting actually changes
AMPscript's FormatNumber function takes a number, a format string, and an optional culture code. It returns a new string built from ordinary digit, separator, and sign characters. The format strings follow .NET conventions:
- N2: grouped digits with two decimal places.
- N0: grouped digits with no decimals.
- C2: currency with two decimal places.
- P: percentage.
- Culture codes such as en-US, de-DE, or fr-FR: control which characters act as the decimal and grouping separators.
%%[
SET @amount = 1234567.891
SET @us = FormatNumber(@amount, "N2", "en-US")
SET @de = FormatNumber(@amount, "N2", "de-DE")
]%%
US: %%=v(@us)=%%
DE: %%=v(@de)=%%
Under standard .NET culture data, this example should print 1,234,567.89 for en-US and 1.234.567,89 for de-DE. In both cases the value is rounded to two places and separators are inserted. Treat these as expected outputs to confirm in preview, not as verified results. For currency specifically, FormatCurrency takes a culture code and, optionally, a precision and a currency symbol.
Three changes happen at once:
- The stored characters change. Separators are real characters in the output, not visual styling.
- Precision may change through rounding.
- The data type changes from number to string.
Formatting does not reverse, mirror, or restyle digits. Words like reverse, mirror, or font do not describe it. In particular, it is unrelated to Unicode styled-digit generators, which substitute characters such as mathematical bold digits. Those characters look like numbers but are not parsed as numbers by most software.
Right-to-left languages add one more consideration. In Arabic or Hebrew text, the Unicode bidirectional algorithm normally displays digit runs left to right. That is a display behavior; the character order in the output stays the same. Whether a given right-to-left culture code produces Latin or native digits in AMPscript was not confirmed in the supplied evidence, so test it before relying on it.
Will the result stay readable and usable after copying or sharing?
Because the output is ordinary text, copying it from an email or page keeps the same characters. The risks appear when someone or something tries to use the copied value as a number.
- Parsing: a spreadsheet or form set to one locale may read 1.234 as one point two three four rather than one thousand two hundred thirty-four. It may also treat a grouped number as text.
- Space separators: some cultures, such as French, group digits with a space. Depending on the culture data in use, that space may be a regular, no-break, or narrow no-break space, and some parsers reject the no-break variants. The exact character AMPscript emits for these cultures was not established in the supplied evidence. Inspect the output if this matters.
- Search and matching: a reader searching their inbox for 1234567 may not find 1,234,567, because the strings differ.
On accessibility, screen readers generally interpret numbers using the language declared for the content. A German-formatted number inside content declared as English could be announced incorrectly. This is a general principle; no specific screen reader or email client was tested for this article. Two steps help:
- Set the language attribute to match the culture code you format with.
- Check pronunciation with the assistive technology your audience uses.
Provide a plain-text equivalent, either an unformatted value or one formatted for a known locale, whenever the number will be:
• copied into another system
• used as a reference code
• passed in a link
• read by software rather than a person
Gaps to check before relying on this
- Rounding mode for midpoint values, such as .005, was not confirmed. Test edge cases that affect money.
- Behavior when the input is empty, null, or non-numeric was not documented in the supplied evidence. Guard inputs with checks such as Empty or IsNull before formatting.
- The exact separator characters for space-grouping and right-to-left cultures should be verified in your account's output.