love python
love python

Reputation: 35

Float value is too long when rendered in Jinja template

I have some float values that represent percentages, but they have too many decimal places. How can I display a shorter value in my Jinja template?

{% for item in items %}
{{ item.name }}
{{ item.score }}%
{% endfor %}
person
10.2419833200485471%

I'd like to display "10.24%" instead.

Upvotes: 0

Views: 402

Answers (1)

Doobeh
Doobeh

Reputation: 9440

You can use the round filter. The documentation shows some ways you can control the rounding.

{{ item.score|round(2) }}

Upvotes: 1

Related Questions