FormatNumber
This function formats a string as a number.
Format patterns follow the C# format types outlined in the official Microsoft documentation:
Some of these custom formats are supported:
Arguments
FormatNumber(1,2,3)
Ordinal | Type | Required | Description |
---|---|---|---|
1 | String | True | The number string to format |
2 | String | True | The output format pattern (see table below for values) |
3 | String | False | ISO code for locale-specific pattern elements |
NOTE: By default, this function rounds half up to two decimal places.
Here are a few examples of format patterns, given a number of 123
:
Element | Format Pattern | Output |
---|---|---|
Currency | C or c |
$123.00 |
Decimal | D or d |
123 |
Exponential (scientific) | E or e |
1.230000E+002 |
Fixed-point | F of f |
123.00 |
General | G or g |
123 |
Number | N or n |
123.00 |
Percent | P or p |
12,300.00 % |
Hexadecimal | X or x |
7B |
NOTE: The
D
pattern requires an integer for input.
Here are a few examples of custom format patterns, given a number of -1.8967
:
Element | Format Pattern | Example | Output |
---|---|---|---|
Zero placeholder | 0 |
"0000" |
-0002 |
Digit placeholder | # |
"###0" |
-2 |
Decimal point | . |
"#0.00" |
-1.90 |
Group separator and scaling | , |
"0,00#.##" |
-0,001.9 |
Percentage placeholder | % |
"#.00%" |
-189.67% |
Exponential notation | E0 , E+0 , e0 or e+0 |
"#0.0e0" |
-19.0e-1 |
Escape character | \ |
"\###0.000\#" |
-#1.897# |
Literal string delimiter | 'string' or "string" |
"#.##' points'" |
-1.9 points |
Section separator | ; |
"#0.00;(#0.00)" |
(1.90) |
Example 1
%%[
var @num
var @Currency
var @Currency3
var @Decimal
var @Exponential
var @Fixed
var @Fixed3
var @General
var @Number
var @Number3
var @Percent
var @Hex
set @num = "123"
set @Currency = FormatNumber(@num, "C")
set @Currency3 = FormatNumber(@num, "C3")
set @Decimal = FormatNumber(@num, "D")
set @Exponential = FormatNumber(@num, "E")
set @Fixed = FormatNumber(@num, "F")
set @Fixed3 = FormatNumber(@num, "F3")
set @General = FormatNumber(@num, "G")
set @Number = FormatNumber(@num, "N")
set @Number3 = FormatNumber(@num, "N3")
set @Percent = FormatNumber(@num, "P")
set @Hex = FormatNumber(@num, "X")
]%%
num: %%=v(@num)=%%
<br><br>Currency: %%=v(@Currency)=%%
<br>Currency3: %%=v(@Currency3)=%%
<br>Decimal: %%=v(@Decimal)=%%
<br>Exponential: %%=v(@Exponential)=%%
<br>Fixed: %%=v(@Fixed)=%%
<br>Fixed3: %%=v(@Fixed3)=%%
<br>General: %%=v(@General)=%%
<br>Number: %%=v(@Number)=%%
<br>Number3: %%=v(@Number3)=%%
<br>Percent: %%=v(@Percent)=%%
<br>Hex: %%=v(@Hex)=%%
Output
num: 123
Currency: $123.00
Currency3: $123.000
Decimal: 123
Exponential: 1.230000E+002
Fixed: 123.00
Fixed3: 123.000
General: 123
Number: 123.00
Number3: 123.000
Percent: 12,300.00 %
Hex: 7B
Example 2
Not a subscriber? Subscribe now.
Example 3
Not a subscriber? Subscribe now.