Formatting money in twig templates

Are there any filters or sth like this in the branch template engine to format money or numbers?

+49
format numbers currency twig
May 07 '11 at 11:57
source share
4 answers

The number_format filter has been included in the Twig kernel since the end of December 2011. The corresponding commit is here .

Usage: number_format(decimals, decimalSeparator, thousandSeparator)

 {{ total|number_format(2) }} {{ total|number_format(0, '.') }} {{ total|number_format(2, '.', ',') }} 

Read more about this in the docs

+76
Feb 01 2018-12-12T00:
source share

The Twig Extensions library contains a number of useful extensions for Twig. With the release of version 1.2.0, the localizedcurrency filter has been added to the Intl extension. As the name suggests, this filter will format a number based on the current locale. For this, the PHP class NumberFormatter .

Using

This filter is very easy to use. The only required argument for the filter is the three-digit currency code ISO 4217 . For example, to display an amount of 27.99 euros, use the following line of code:

 {{ price|localizedcurrency('EUR') }} 

Depending on the locale, different results will be displayed:

  • €27.99 if the locale is set to en
  • 27,99 € if the locale is set to fr
  • € 27,99 If locale is set to nl

Language Installation / Setup

Instructions for installing the Intl extension can be found in this separate answer .

+25
Feb 09 '15 at 12:41
source share
+9
Jul 28 '11 at 7:50
source share

If you are using an older version of the branch and you do not want to install any extensions, you can use the format filter as follows:

 {{ "%.2f"|format(total) }} 

Not very nice, but it works.

+9
Jun 30 '12 at 5:28
source share



All Articles