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.
|number_format
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 }}
@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)) .
//
{{ variable // 1 }}
intval(floor(variable))
From branch 1.15 you can use a round filter.
{{ 2.6|round(0, 'floor') }}
http://twig.sensiolabs.org/doc/filters/round.html