AMPscript – FormatCurrency()

Have you gotten an email with 2 $$’s or worse with none? What about the dreaded .5 instead of .50? Currency is used in all kinds of emails and when it is formatted wrong, it sticks out like a sore thumb. But why does this happen?

Most people store currency as a number, not a string. That means that it doesn’t have the $ sign and will auto-truncate the cents. (This is best practice).

One mistake that I often run across is finding the $ written inline with the text. For example: “Sally had $” followed by the number variable. This gets really messy and can cause issues down the line. Instead, it is cleaner and easier to reformat the number into currency using AMPscript.

The FormatCurrency() function is what you would use. This function allows you to adjust the format of your number to match currency formatting for whatever country/culture you request. For US – this means adding a $ sign, 2 decimal places and period denoting the change. ($1,234.56). Check out the examples at the bottom for my cheat sheet on the most common culture codes:


SYNTAX

FormatCurrency(@number,”culturecode”,#_of_decimalplaces,”symbol”)

Definition of parts — Each one can be passed as a @variable or in “string format”

number= This is the number you want to be changed. (REQUIRED)

CULTURECODE= This is the code identifying what language/culture formatting you are requesting. See below for a cheat-sheet (REQUIRED)

#_OF_DECIMALS= This is the number of decimal places you want. This is optional, and if not included will default to what is customary for that culturecode – for “en-us” that is 2 places

SYMBOL= This allows you to override the symbol to something you define.


Simple Examples:

FormatCurrency(1234.567,”en-US”) → $1234.56
FormatCurrency(1234.567,”en-US”,1) → $1234.6
FormatCurrency(1234.567,”fr_FR”) → 1.234,56 €


Advanced Example 1 – AMPscript Block:

%%[
var @amount
set @amount= "1234.5678"
set @amount= FormatCurrency(@amount, "en-us")
]%%
Total amount = %%=v(@amount)=%%

Advanced Example 2 – Inline:

%%[
VAR @amount, @locale
set @amount= [Amount] 
set @locale="en-us"
]%%
Total amount = %%=formatcurrency(@amount,@locale)=%%

Cheat-sheet of Variables:

United States → %%=FormatCurrency(@productPrice,”en-US”,2,”$”)=%%
Great Britain → %%=FormatCurrency(@productPrice,”en-GB”,2,”£”)=%%
Norway → %%=FormatCurrency(@productPrice,”nb-NO”,2,”kr”)=%%
France → %%=FormatCurrency(@productPrice,”fr-FR”,2,”€”)=%%
Switzerland → %%=FormatCurrency(@productPrice,”de-CH”,2,”CHF”)=%%
Belgium → %%=FormatCurrency(@productPrice,”fr-BE”,2,”€”)=%%
Germany → %%=FormatCurrency(@productPrice,”de-DE”,2,”€”)=%%
Austria → %%=FormatCurrency(@productPrice,”de-AT”,2,”€”)=%%
Netherlands → %%=FormatCurrency(@productPrice,”nl-NL”,2,”€”)=%%
Denmark → %%=FormatCurrency(@productPrice,”da-DK”,2,”DKK”)=%%
Finland → %%=FormatCurrency(@productPrice,”fi-FI”,2,”€”)=%%
Sweden → %%=FormatCurrency(@productPrice,”sv-SE”,2,”kr”)=%%
Australia → %%=FormatCurrency(@productPrice,”en-AU”,2,”$”)=%%

Full list of available culture codes and formatting


References:

Salesforce Developer – FormatCurrency()

AMPscript Guide – FormatCurrency()

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.