Ampscript Formatting Functions Guide

AMPscript Formatting Functions Guide

AMPscript Formatting Functions Guide

Master AMPscript functions: Format, FormatCurrency, and FormatNumber for Salesforce Marketing Cloud.

Introduction

AMPscript is the cornerstone of personalization and dynamic content in Salesforce Marketing Cloud (SFMC). This guide provides an in-depth explanation of the key formatting functions—Format, FormatCurrency, and FormatNumber. These functions allow precise control over the display of strings, numbers, dates, and currencies in your emails and landing pages.

1. Format Function

The Format function formats a string into a specific pattern based on type and locale. It supports various patterns, including standard numeric and date-time formats.

Syntax

Format(1, 2, 3, 4)
  • 1: The string to format (e.g., date or number).
  • 2: The format pattern (e.g., D, C, MMM).
  • 3: (Optional) Data type (Date or Number).
  • 4: (Optional) ISO locale code (e.g., en-US, fr-FR).

Examples

Date Formatting


%%[
    SET @dateString = "2017-09-15T06:07:08.1230000-06:00"
    SET @long = Format(@dateString, "D", "Date")
    SET @short = Format(@dateString, "d", "Date")
    SET @rfc = Format(@dateString, "R", "Date")
    SET @universal = Format(@dateString, "U", "Date")
]%%
        

Output:

  • Long Date (D): Friday, September 15, 2017
  • Short Date (d): 9/15/2017
  • RFC1123 (R): Fri, 15 Sep 2017 06:07:08 GMT
  • Universal Time (U): Friday, September 15, 2017 12:07:08 PM

Number Formatting


%%[
    SET @num = -1.8967
    SET @currency = Format(@num, "C", "Number")
    SET @percent = Format(@num, "P", "Number")
    SET @custom = Format(@num, "#,##0.00", "Number")
]%%
        

Output:

  • Currency (C): ($1.90)
  • Percent (P): -189.67%
  • Custom (#,##0.00): -1,897.00

2. FormatCurrency Function

The FormatCurrency function formats a string as a currency according to the specified locale. It allows you to define decimal precision and optionally override the currency symbol.

Syntax

FormatCurrency(1, 2, 3, 4)
  • 1: The currency string to format.
  • 2: The ISO locale code for the currency (e.g., en-US, fr-FR).
  • 3: (Optional) Decimal places for the output.
  • 4: (Optional) Currency symbol to override the default for the locale.

Examples


%%[
    SET @num = "123.45678"
    SET @locale = "en-US"
    SET @currency = FormatCurrency(@num, @locale)
]%%
        

Output:

  • Number: 123.45678
  • Locale: en-US
  • Formatted Currency: $123.46

3. FormatNumber Function

The FormatNumber function formats a string as a number using various patterns, including standard numeric, scientific, and custom formats. By default, this function rounds half up to two decimal places.

Syntax

FormatNumber(1, 2, 3)
  • 1: The number string to format.
  • 2: The format pattern (e.g., C, D, N).
  • 3: (Optional) ISO locale code for locale-specific patterns.

Examples


%%[
    SET @num = "123"
    SET @currency = FormatNumber(@num, "C")
    SET @exponential = FormatNumber(@num, "E")
    SET @percent = FormatNumber(@num, "P")
    SET @hexadecimal = FormatNumber(@num, "X")
]%%
        

Output:

  • Currency (C): $123.00
  • Exponential (E): 1.230000E+002
  • Percent (P): 12,300.00%
  • Hexadecimal (X): 7B

A voir: