70

Are there any filters or something like that in twig template engine to format money or numbers?

Roberto Caboni
  • 6,078
  • 10
  • 19
  • 34
umpirsky
  • 9,358
  • 12
  • 65
  • 92
  • 2
    Since 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 Answers4

115

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

Jrgns
  • 22,631
  • 17
  • 67
  • 75
  • 3
    Is there no way to do this user locale sensitive? – fnagel Nov 26 '13 at 13:55
  • And this is the link to the doc http://twig.sensiolabs.org/doc/filters/number_format.html – Tib Jun 11 '14 at 08:11
  • @Tib Added the link, thanx! – Jrgns Jul 15 '14 at 03:53
  • 10
    Amazingly, 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
    @VitaliyZurian: It’s only expected if you know the operator precedence. – Timwi Sep 04 '15 at 18:23
  • 1
    @Timwi thanks a lot, I was running into the same problem – TSGames Oct 09 '17 at 18:59
37

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 to en
  • 27,99 € if the locale is set to fr
  • € 27,99 if the locale is set to nl

Installation / setting the locale

Installation instructions for the Intl extension can be found in this seperate answer.

Community
  • 1
  • 1
Nic Wortel
  • 10,515
  • 5
  • 56
  • 79
  • How should one disable the `Thousand Separator` in `localizednumber` ?? – Pmpr 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
  • More gooder than number_format method I think, but both work just fine – David Sep 27 '19 at 15:56
  • 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
11

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

Jens
  • 5,066
  • 5
  • 46
  • 64
  • I've used this and can confirm that it works. Can you explain how it works? – Sam Jan 08 '15 at 16:20
  • 2
    No 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
1

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

Panagiotis Koursaris
  • 3,025
  • 2
  • 20
  • 40
  • 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