2

For example I have 19.796 and I need to round it up so I use it like this {% set final_sum = sum + (sum / 100 * 21) | number_format(2, 'ceil') %} and it returns me 19.8 but I need 19.80 with zero on the end.

Kin
  • 4,466
  • 13
  • 54
  • 106

5 Answers5

3

You can try this:

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

or localized currency:

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

UPD:

in your case you can do something like that:

{% set final_sum = "%.2f" | format(sum + (sum / 100 * 21)) %}

So then you just can use this already formatted value as {{ final_sum }}

spiilmusic
  • 717
  • 3
  • 9
  • 24
  • Is it possible to use it with `set` as i need to use `final_sum` in many places – Kin Jun 07 '17 at 12:34
0

The second parameter of number_format is decimal point
I don't know twig but a look in the docs tell me that you should try this code

{% set final_sum = ceil(sum + (sum / 100 * 21))|number_format(2) %}
UfguFugullu
  • 2,107
  • 13
  • 18
0
{{ '%.2f'|format(19.796) }}

it will round it to 19.80

Arun
  • 1,609
  • 1
  • 15
  • 18
0

use this.

{{ "%.2f"|format(19.796|round(2, 'ceil')) }} //19.80

19.8 is same as 19.80 so if you want with trailing 0 then convert it into a string.

0
{{ "%.2f"|format(19.796) }}

as a very basic approach... :) prints out 19.80

nomad
  • 1
  • 2