3

The Twig documentation for number rounding talks about rounding decimals, but I have a case where I want to round a number like 19,995 to 20,000. Is there a tricky way to round up to nearest thousand?

ArleyM
  • 855
  • 5
  • 15

2 Answers2

4

The round filter accepts negative precision. (Just like in PHP)

{{ 19995|round(-3) }}
Community
  • 1
  • 1
Matt Rose
  • 515
  • 3
  • 8
3

The simplest way is to divide your original number, round, then multiply again by 1000:

{% set amount = (19995 / 1000)|round %}
{{ amount * 1000 }}

Rounding to the nearest 10k or 100k would be as simple as changing 1000 to 10000 or 100000 respectively.

UPDATE: Or you can just use Matt Rose's suggestion below.

{{ 19995|round(-3) }}
tmirks
  • 503
  • 5
  • 11