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.
Asked
Active
Viewed 1,326 times
2

Kin
- 4,466
- 13
- 54
- 106
-
Try this : https://stackoverflow.com/a/4483561/2815635 – Niklesh Raut Jun 07 '17 at 12:25
-
Possible duplicate of [PHP: show a number to 2 decimal places](https://stackoverflow.com/questions/4483540/php-show-a-number-to-2-decimal-places) – caramba Jun 07 '17 at 12:26
-
Did you mean $price = 19.796; echo $price = number_format(round($price, 2), 2); – Arun Jun 07 '17 at 12:28
-
1The main problem is that I need to do this in twig. – Kin Jun 07 '17 at 12:29
-
Guys, this question about twig. – spiilmusic Jun 07 '17 at 12:29
5 Answers
3
You can try this:
{{ "%.2f"|format(final_sum) }}
{{ 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
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.

Vinay Khobragade
- 596
- 4
- 7