user519846
user519846

Reputation: 1009

How to round a value in Twig

I want to round a value in Twig.

Example: I want to display 80.5555 as 80.55.

Can any one suggest me how to do that?

Upvotes: 23

Views: 14946

Answers (3)

pastorello
pastorello

Reputation: 1022

Twig documentation says that there's a filter for this task, since 1.15.0:

just write {{80.5555|round}}

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

Upvotes: 3

Elnur Abdurrakhimov
Elnur Abdurrakhimov

Reputation: 44841

You could use the format filter for that:

{{ '%.2f'|format(80.5555) }}

But note that it will round it to 80.56.

Upvotes: 30

jonfer
jonfer

Reputation: 1564

{{ 80.5555 | number_format(2) }}

Here is the documentation number_format

Upvotes: 32

Related Questions