Twig Components / Methods / number_format
Note: You are currently reading the documentation for Bolt 5.0. Looking for the documentation for Bolt 5.2 instead?
number_format(decimal = null, decimalPoint = null, thousandSep = null)
is a Twig filter to format numbers. It is a
wrapper around PHP's number_format function:
{{ 200.35|number_format }}
You can control the number of decimal places, decimal point, and thousands separator using the additional arguments:
{{ 9800.333|number_format(2, '.', ',') }}
To format negative numbers or math calculation, wrap the previous statement with parentheses (needed because of Twig's precedence of operators):
{{ -9800.333|number_format(2, '.', ',') }} {# outputs : -9 #}
{{ (-9800.333)|number_format(2, '.', ',') }} {# outputs : -9,800.33 #}
{{ 1 + 0.2|number_format(2) }} {# outputs : 1.2 #}
{{ (1 + 0.2)|number_format(2) }} {# outputs : 1.20 #}
If no formatting options are provided then Twig will use the default formatting options of:
- 0 decimal places.
- . as the decimal point.
- , as the thousands separator.
These defaults can be changed through the core extension:
$twig = new \Twig\Environment($loader);
$twig->getExtension(\Twig\Extension\CoreExtension::class)->setNumberFormat(3, '.', ',');
The defaults set for number_format can be over-ridden upon each call using the additional parameters.
ArgumentsΒΆ
- decimal: The number of decimal points to display
- decimal_point: The character(s) to use for the decimal point
- thousand_sep: The character(s) to use for the thousands separator
Source: Twig
Couldn't find what you were looking for? We are happy to help you in the forum, on Slack or on Github.