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.
@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))
.
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 }}
Since twig 1.15, you can use round filter.
{{ 2.6|round(0, 'floor') }}
https://twig.symfony.com/doc/1.x/filters/round.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With