How to round to zero decimals if there is no decimal value with Jinja2?
Answer a question
I building a website using the (excellent) Flask framework in which I now want to display some numbers. The round filter provided by jinja2 works fine, except for when there is no decimal value:
{{ 1.55555|round(2) }} -> 1.56
{{ 1.5|round(2) }} -> 1.5
{{ 1.0|round(2) }} -> 1.0
{{ 1|round(2) }} -> 1.0
But I want the last two to be displayed like 1 (without a trailing .0). Does anybody know how I can do this with jinja2? All tips are welcome!
[EDIT]
I tried using trim(), but to my surprise the snippet below gives a TypeError: do_trim() takes exactly 1 argument (2 given):
{{ 1.0|round(2)|trim('.0') }}
Answers
You can use string filter, then use str.rstrip:
>>> import jinja2
>>> print(jinja2.Template('''
... {{ (1.55555|round(2)|string).rstrip('.0') }}
... {{ (1.5|round(2)|string).rstrip('.0') }}
... {{ (1.0|round(2)|string).rstrip('.0') }}
... {{ (1|round(2)|string).rstrip('.0') }}
... ''').render())
1.56
1.5
1
1
NOTE
Using str.rstrip, you will get an empty string for 0.
>>> jinja2.Template('''{{ (0|round(2)|string()).strip('.0') }}''').render()
u''
Here's a solution to avoid above (call rstrip twice; once with 0, once with .)
>>> print(jinja2.Template('''
... {{ (1.55555|round(2)|string).rstrip('0').rstrip('.') }}
... {{ (1.5|round(2)|string).rstrip('0').rstrip('.') }}
... {{ (1.0|round(2)|string).rstrip('0').rstrip('.') }}
... {{ (1|round(2)|string).rstrip('0').rstrip('.') }}
... {{ (0|round(2)|string).rstrip('0').rstrip('.') }}
... ''').render())
1.56
1.5
1
1
0
UPDATE Above codes will trim 10 to 1. Following code does not have the issue. using format filter.
>>> print(jinja2.Template('''
... {{ ("%.2f"|format(1.55555)).rstrip('0').rstrip('.') }}
... {{ ("%.2f"|format(1.5)).rstrip('0').rstrip('.') }}
... {{ ("%.2f"|format(1.5)).rstrip('0').rstrip('.') }}
... {{ ("%.2f"|format(1.0)).rstrip('0').rstrip('.') }}
... {{ ("%.2f"|format(0)).rstrip('0').rstrip('.') }}
... {{ ("%.2f"|format(10)).rstrip('0').rstrip('.') }}
... ''').render())
1.56
1.5
1.5
1
0
10
更多推荐

所有评论(0)