'Rounding numbers in Twig

Does someone know how to round down numbers in Twig to the nearest whole number?

Ex : 2.6 => 2

I tried to use |number_format but it doesn't round down them.



Solution 1:[1]

@olivierw's answer is correct, but there's another trick you can use. Twig does have // operator which floors down result of division. You can use it as {{ variable // 1 }} which equals to intval(floor(variable)).

Solution 2:[2]

Since twig 1.15, you can use round filter.

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

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bobbel
Solution 2 bobbel