Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to round a value in Twig

I want to round a value in Twig.

Example: I want to display 80.5555 as 80.55.

Can any one suggest me how to do that?

like image 569
user519846 Avatar asked Apr 03 '12 10:04

user519846


3 Answers

{{ 80.5555 | number_format(2) }}

Here is the documentation number_format

like image 112
jonfer Avatar answered Nov 19 '22 15:11

jonfer


You could use the format filter for that:

{{ '%.2f'|format(80.5555) }}

But note that it will round it to 80.56.

like image 45
Elnur Abdurrakhimov Avatar answered Nov 19 '22 15:11

Elnur Abdurrakhimov


Twig documentation says that there's a filter for this task, since 1.15.0:

just write {{80.5555|round}}

https://twig.symfony.com/doc/1.x/filters/round.html

like image 3
pastorello Avatar answered Nov 19 '22 16:11

pastorello