blob: a1c2804ab4bd064d3ce80ac84fdfbd673bcef247 [file] [log] [blame]
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01001``format_number``
2=================
3
4The ``format_number`` filter formats a number:
5
6.. code-block:: twig
7
8 {{ '12.345'|format_number }}
9
10You can pass attributes to tweak the output:
11
12.. code-block:: twig
13
14 {# 12.34 #}
15 {{ '12.345'|format_number({rounding_mode: 'floor'}) }}
16
17 {# 1000000.0000 #}
18 {{ '1000000'|format_number({fraction_digit: 4}) }}
19
20The list of supported options:
21
22* ``grouping_used``;
23* ``decimal_always_shown``;
24* ``max_integer_digit``;
25* ``min_integer_digit``;
26* ``integer_digit``;
27* ``max_fraction_digit``;
28* ``min_fraction_digit``;
29* ``fraction_digit``;
30* ``multiplier``;
31* ``grouping_size``;
32* ``rounding_mode``;
33* ``rounding_increment``;
34* ``format_width``;
35* ``padding_position``;
36* ``secondary_grouping_size``;
37* ``significant_digits_used``;
38* ``min_significant_digits_used``;
39* ``max_significant_digits_used``;
40* ``lenient_parse``.
41
42Besides plain numbers, the filter can also format numbers in various styles:
43
44.. code-block:: twig
45
46 {# 1,234% #}
47 {{ '12.345'|format_number(style='percent') }}
48
49 {# twelve point three four five #}
50 {{ '12.345'|format_number(style='spellout') }}
51
52 {# 12 sec. #}
53 {{ '12'|format_duration_number }}
54
55The list of supported styles:
56
57* ``decimal``;
58* ``currency``;
59* ``percent``;
60* ``scientific``;
61* ``spellout``;
62* ``ordinal``;
63* ``duration``.
64
65As a shortcut, you can use the ``format_*_number`` filters by replacing `*` with
66a style:
67
68.. code-block:: twig
69
70 {# 1,234% #}
71 {{ '12.345'|format_percent_number }}
72
73 {# twelve point three four five #}
74 {{ '12.345'|format_spellout_number }}
75
76You can pass attributes to tweak the output:
77
78.. code-block:: twig
79
80 {# 12.3% #}
81 {{ '0.12345'|format_percent_number({rounding_mode: 'floor', fraction_digit: 1}) }}
82
83By default, the filter uses the current locale. You can pass it explicitly:
84
85.. code-block:: twig
86
87 {# 12,345 #}
88 {{ '12.345'|format_number(locale='fr') }}
89
90.. note::
91
92 The ``format_number`` filter is part of the ``IntlExtension`` which is not
93 installed by default. Install it first:
94
95 .. code-block:: bash
96
97 $ composer require twig/intl-extra
98
99 Then, on Symfony projects, install the ``twig/extra-bundle``:
100
101 .. code-block:: bash
102
103 $ composer require twig/extra-bundle
104
105 Otherwise, add the extension explicitly on the Twig environment::
106
107 use Twig\Extra\Intl\IntlExtension;
108
109 $twig = new \Twig\Environment(...);
110 $twig->addExtension(new IntlExtension());
111
112Arguments
113---------
114
115* ``locale``: The locale
116* ``attrs``: A map of attributes
117* ``style``: The style of the number output