Can Twig round numbers to the nearest thousand?
Twig documentation for rounding a number talks about rounding decimal places, but I have a case where I want to round a number, for example from 19.995 to 20,000. Is there a tricky way to round to the nearest thousand?
+3
source to share
2 answers
The easiest way is to divide the original number, round, and then multiply by 1000 again:
{% set amount = (19995 / 1000)|round %}
{{ amount * 1000 }}
Rounding to the nearest 10k or 100k would be as easy as changing 1000 to 10000 or 100000 respectively.
UPDATE: Or you can just use Matt Rose's suggestion below.
{{ 19995|round(-3) }}
+3
source to share