Format

The format function can be used to format data. Some common examples include formatting currencies, decimal numbers, or dates.

Syntax

string format(number|string|datetime $data, string $format, string $culture)

This function takes a numerical-, string- or date value as $data, and formats this input based on $format and $culture. Output is a string value.

Parameters

$data: Can be a number, string, or date-time object.
$format: A string defining how the $data should be formatted. Should follow Microsoft standard format specifiers (e.g., “C2”, “N1”).
$culture: A string defining the culture the $data formatting should follow. Should follow iana values (e.g. “en-US”, “es-ES”).

Example

Below, “FirstValue” is formatted in four different ways showcasing various options.

{
    "FirstValue": 1000
}

The names of the values below indicate what the transformation is achieving.

{
    AmericanCurrencyTwoDecimals: format(FirstValue, 'C2', 'en-US'),
    SpanishCurrencyNoDecimals: format(FirstValue, 'C0', 'es-ES'),
    AmericanNumberTwoDecimals: format(FirstValue, 'N2', 'en-US'),
    SpanishNumberTwoDecimals: format(FirstValue, 'N2', 'es-ES')
}

Thus, currencies based on culture, number of decimals, thousands separator, and decimal points are controlled.

{
    "AmericanCurrencyTwoDecimals": "$1,000.00",
    "SpanishCurrencyNoDecimals": "1.000 €",
    "AmericanNumberTwoDecimals": "1,000.00",
    "SpanishNumberTwoDecimals": "1.000,00"
}

There are more formatting options available than those showcased here. Please look through the Microsoft standard format specifiers for additional formatting options.