76

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

1
  • 2
    Since the release of Twig Extensions 1.2.0 in October 2014, a localizedcurrency filter is available to format money in Twig, based on the locale. See my answer for more information.
    – Nic Wortel
    Feb 9, 2015 at 13:08

4 Answers 4

130

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

6
  • 3
    Is there no way to do this user locale sensitive?
    – fnagel
    Nov 26, 2013 at 13:55
  • 12
    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 2, 2014 at 14:16
  • 1
    @Timwi that's kind of expected since you're piping the total to the twig filter Aug 27, 2015 at 14:30
  • 4
    @VitaliyZurian: It’s only expected if you know the operator precedence.
    – Timwi
    Sep 4, 2015 at 18:23
  • 1
    @Timwi thanks a lot, I was running into the same problem
    – TSGames
    Oct 9, 2017 at 18:59
39

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.

5
  • How should one disable the Thousand Separator in localizednumber ?? May 15, 2017 at 6: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, 2017 at 8:45
  • More gooder than number_format method I think, but both work just fine
    – David
    Sep 27, 2019 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 9, 2020 at 13:57
  • Found it! localizednumber is great, but format_number (do not confuse with number_format) is awesome. More info here: stackoverflow.com/questions/58662974/…
    – xarlymg89
    Mar 9, 2020 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

2
  • I've used this and can confirm that it works. Can you explain how it works?
    – Sam
    Jan 8, 2015 at 16:20
  • 2
    No need, I've worked it out. format works like PHP's sprintf function and that can do currency formatting.
    – Sam
    Jan 8, 2015 at 16:26
2

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

1
  • 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 5, 2020 at 3:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.