Jinja: buitin filters `round` and `int` do not work

Created on 14 Nov 2013  ·  4Comments  ·  Source: pallets/jinja

I'm using Jinja2==2.7.1

In [2]: t = jinja2.Template("{{ x/2 | round }}") #does not work?

In [3]: t
Out[3]: <Template memory:161e910>

In [4]: t.render(x=7)
Out[4]: u'3.5'

In [5]: t2 = jinja2.Template("{{ x | float }}") # float works

In [6]: t2.render(x=3)
Out[6]: u'3.0'

In [12]: t3 = Template("{{ x / 2 | int }}") # does not work

In [13]: t3.render(x=7)
Out[13]: u'3.5'

Most helpful comment

My guess would be operator precedence. x/2 | round is parsed as x / (2 | round)

All 4 comments

My guess would be operator precedence. x/2 | round is parsed as x / (2 | round)

yes, you are right,
Thanks

In [6]: t = jinja2.Template("{{ (x/2) | int }}")

In [7]: t.render(x=5)
Out[7]: u'2'

Seem it relate to this one https://github.com/mitsuhiko/jinja2/issues/119

Just ran into the exact problem!
Thanks @hvnsweeting and @Naddiseo

Same issue, same solution. In my case, it was in Home Assistant where I ended up with:
{{ ((states.input_number.guestroom_setpoint_shift.state | float) * 2) | int }}
Thanks!

Was this page helpful?
0 / 5 - 0 ratings