1``number_format``
2=================
3
4.. versionadded:: 1.5
5    The ``number_format`` filter was added in Twig 1.5
6
7The ``number_format`` filter formats numbers.  It is a wrapper around PHP's
8`number_format`_ function:
9
10.. code-block:: jinja
11
12    {{ 200.35|number_format }}
13
14You can control the number of decimal places, decimal point, and thousands
15separator using the additional arguments:
16
17.. code-block:: jinja
18
19    {{ 9800.333|number_format(2, '.', ',') }}
20
21To format negative numbers, wrap the number with parentheses (needed because of
22Twig's :ref:`precedence of operators <twig-expressions>`:
23
24.. code-block:: jinja
25
26    {{ -9800.333|number_format(2, '.', ',') }} {# outputs : -9 #}
27    {{ (-9800.333)|number_format(2, '.', ',') }} {# outputs : -9,800.33 #}
28
29If no formatting options are provided then Twig will use the default formatting
30options of:
31
32* 0 decimal places.
33* ``.`` as the decimal point.
34* ``,`` as the thousands separator.
35
36These defaults can be easily changed through the core extension:
37
38.. code-block:: php
39
40    $twig = new \Twig\Environment($loader);
41    $twig->getExtension('\Twig\Extension\CoreExtension')->setNumberFormat(3, '.', ',');
42
43    // before Twig 1.26
44    $twig->getExtension('core')->setNumberFormat(3, '.', ',');
45
46The defaults set for ``number_format`` can be over-ridden upon each call using the
47additional parameters.
48
49Arguments
50---------
51
52* ``decimal``:       The number of decimal points to display
53* ``decimal_point``: The character(s) to use for the decimal point
54* ``thousand_sep``:   The character(s) to use for the thousands separator
55
56.. _`number_format`: https://secure.php.net/number_format
57