The number of roundings in Twig

Does anyone know how to round numbers in Twig to the nearest integer?

Ex: 2.6 => 2

I tried using |number_format , but it does not round them.

+7
source share
3 answers

Follow the instructions on this page to create your own filter:

 $twig = new Twig_Environment($loader); $twig->addFilter('floor', new Twig_Filter_Function('floor')); 

Then in your template:

 {{ myNumber|floor }} 
+11
source

@olivierw's answer is correct, but there is another trick you can use. Twig has an // operator that reduces the result of division. You can use it as {{ variable // 1 }} , which is equal to intval(floor(variable)) .

+24
source

From branch 1.15 you can use a round filter.

 {{ 2.6|round(0, 'floor') }} 

http://twig.sensiolabs.org/doc/filters/round.html

+4
source

All Articles