Matthias Andreas Benkard | 12a5735 | 2021-12-28 18:02:04 +0100 | [diff] [blame^] | 1 | ``format_currency`` |
| 2 | =================== |
| 3 | |
| 4 | The ``format_currency`` filter formats a number as a currency: |
| 5 | |
| 6 | .. code-block:: twig |
| 7 | |
| 8 | {# €1,000,000.00 #} |
| 9 | {{ '1000000'|format_currency('EUR') }} |
| 10 | |
| 11 | You can pass attributes to tweak the output: |
| 12 | |
| 13 | .. code-block:: twig |
| 14 | |
| 15 | {# €12.34 #} |
| 16 | {{ '12.345'|format_currency('EUR', {rounding_mode: 'floor'}) }} |
| 17 | |
| 18 | {# €1,000,000.0000 #} |
| 19 | {{ '1000000'|format_currency('EUR', {fraction_digit: 4}) }} |
| 20 | |
| 21 | The list of supported options: |
| 22 | |
| 23 | * ``grouping_used``; |
| 24 | * ``decimal_always_shown``; |
| 25 | * ``max_integer_digit``; |
| 26 | * ``min_integer_digit``; |
| 27 | * ``integer_digit``; |
| 28 | * ``max_fraction_digit``; |
| 29 | * ``min_fraction_digit``; |
| 30 | * ``fraction_digit``; |
| 31 | * ``multiplier``; |
| 32 | * ``grouping_size``; |
| 33 | * ``rounding_mode``; |
| 34 | * ``rounding_increment``; |
| 35 | * ``format_width``; |
| 36 | * ``padding_position``; |
| 37 | * ``secondary_grouping_size``; |
| 38 | * ``significant_digits_used``; |
| 39 | * ``min_significant_digits_used``; |
| 40 | * ``max_significant_digits_used``; |
| 41 | * ``lenient_parse``. |
| 42 | |
| 43 | By default, the filter uses the current locale. You can pass it explicitly: |
| 44 | |
| 45 | .. code-block:: twig |
| 46 | |
| 47 | {# 1.000.000,00 € #} |
| 48 | {{ '1000000'|format_currency('EUR', locale='de') }} |
| 49 | |
| 50 | .. note:: |
| 51 | |
| 52 | The ``format_currency`` filter is part of the ``IntlExtension`` which is not |
| 53 | installed by default. Install it first: |
| 54 | |
| 55 | .. code-block:: bash |
| 56 | |
| 57 | $ composer require twig/intl-extra |
| 58 | |
| 59 | Then, on Symfony projects, install the ``twig/extra-bundle``: |
| 60 | |
| 61 | .. code-block:: bash |
| 62 | |
| 63 | $ composer require twig/extra-bundle |
| 64 | |
| 65 | Otherwise, add the extension explicitly on the Twig environment:: |
| 66 | |
| 67 | use Twig\Extra\Intl\IntlExtension; |
| 68 | |
| 69 | $twig = new \Twig\Environment(...); |
| 70 | $twig->addExtension(new IntlExtension()); |
| 71 | |
| 72 | Arguments |
| 73 | --------- |
| 74 | |
| 75 | * ``currency``: The currency |
| 76 | * ``attrs``: A map of attributes |
| 77 | * ``locale``: The locale |