Are there any filters or something like that in twig template engine to format money or numbers?
-
2Since the release of Twig Extensions 1.2.0 in October 2014, a [`localizedcurrency` filter](http://twig.sensiolabs.org/doc/extensions/intl.html#localizedcurrency) is available to format money in Twig, based on the locale. See [my answer](http://stackoverflow.com/a/28410078/1001110) for more information. – Nic Wortel Feb 09 '15 at 13:08
4 Answers
The number_format
filter has been included in the Twig core since the end of December 2011. The relevant commit is here.
Usage: number_format(decimals, decimalSeparator, thousandSeparator)
{{ total|number_format(2) }}
{{ total|number_format(0, '.') }}
{{ total|number_format(2, '.', ',') }}
Read more about it in the docs

- 411
- 2
- 6

- 24,699
- 18
- 71
- 77
-
3
-
And this is the link to the doc http://twig.sensiolabs.org/doc/filters/number_format.html – Tib Jun 11 '14 at 08:11
-
12Amazingly, none of the documentation and none of the SO answers hint that you have to parenthesise the first argument if it’s a formula. I had `{{ 100*done/total | number_format(2) }}` and it took me a good 30 minutes to figure out why the result was wrong. You have to write `{{ (100*done/total) | number_format(2) }}`. – Timwi Sep 02 '14 at 14:16
-
1@Timwi that's kind of expected since you're piping the `total` to the twig filter – Vitalii Zurian Aug 27 '15 at 14:30
-
4
-
1
The Twig Extensions library contains a number of useful extensions for Twig. With the release of version 1.2.0, a localizedcurrency
filter has been added to the Intl extension. As the name suggests, this filter will format a number based on the current locale. It uses PHP's NumberFormatter
class to do so.
Usage
This filter is very easy to use. The only required argument for the filter is the 3-letter ISO 4217 currency code. For instance, to display an amount of 27.99 in Euros, use the following line of code:
{{ price|localizedcurrency('EUR') }}
This will display different results depending on the locale:
€27.99
if the locale is set toen
27,99 €
if the locale is set tofr
€ 27,99
if the locale is set tonl
Installation / setting the locale
Installation instructions for the Intl extension can be found in this seperate answer.

- 1
- 1

- 11,155
- 6
- 60
- 79
-
How should one disable the `Thousand Separator` in `localizednumber` ?? – Peyman Mohamadpour May 15 '17 at 06:00
-
1@Trix interesting question. This is possible with PHP's `NumberFormatter`, but you will have to write a custom Twig Extension because the `localizedcurrency` filter does not support it. I think this deserves its own Stack Overflow question though. If you post the question and send me the link, I will post my answer there. – Nic Wortel May 17 '17 at 08:45
-
-
I'm delighted with this localizedcurrency, localizeddate and localizednumber filters. However, in the case of the localizednumber I really would love to define a fixed amount of decimals. At the moment, I see the numbers using a number of decimals that is best approached by the filter, but it doesn't show a uniform output. In the same column there can be 0, 1, 2, 3 decimals... In the documentation I cannot manage to set a way for handling this. Any idea??? @NicWortel – xarlymg89 Mar 09 '20 at 13:57
-
Found it! localizednumber is great, but format_number (do not confuse with number_format) is awesome. More info here: https://stackoverflow.com/questions/58662974/how-to-increase-decimal-precision-on-percentage-format-using-twigs-intl-extensi – xarlymg89 Mar 09 '20 at 14:12
If you are using an older version of twig and you don't want to install any extensions you can use the format filter like this:
{{ "%.2f"|format(total) }}
Not very nice, but it works.
Basically format
works like PHP's sprintf
function

- 5,767
- 5
- 54
- 69
-
I've used this and can confirm that it works. Can you explain how it works? – Sam Jan 08 '15 at 16:20
-
2No need, I've worked it out. `format` works like PHP's `sprintf` function and that [can do currency formatting](http://php.net/sprintf#example-4997). – Sam Jan 08 '15 at 16:26
Use the format_currency
From version 2.12 format_currency
filter was added. More information in the official documentation https://twig.symfony.com/doc/2.x/filters/format_currency.html

- 3,794
- 4
- 23
- 46
-
The `format_currency` filter is quite bad tho, as it is common to store money as integers, and the filter show the whole integer ... – GondyB May 05 '20 at 03:24