Jinja2 round filter not rounding
Question:
I have the following code in my template:
data: [{% for deet in deets %} {{ deet.value*100|round(1) }}{% if not loop.last %},{% endif %} {% endfor %}]
I am expecting data rounded to 1 decimal place. However, when I view the page or source, this is the output I’m getting:
data: [ 44.2765833818, 44.2765833818, 44.2765833818, 44.2765833818, 44.2765833818, 44.2765833818, 44.2765833818, 44.2765833818, 44.2765833818, 44.2765833818 ]
This is not rounded to 1 decimal place. It runs without a template error or anything, but produces incorrect output. My understanding from the documentation, and even a related stack overflow question, are that my format should work. What am I missing or doing wrong?
Answers:
Didn’t realize the filter operator had precedence over multiplication!
Following up on bernie’s comment, I switched
{{ deet.value*100|round(1) }}
to
{{ 100*deet.value|round(1) }}
which solved the problem. I agree the processing should happen in the code elsewhere, and that would be better practice.
You can put parens around the value that you want to round. (This works for division as well, contrary to what @sobri wrote.)
{{ (deet.value/100)|round }}
NOTE: round
returns a float
so if you really want the int
you have to pass the value through that filter as well.
{{ (deet.value/100)|round|int }}
I ran across this… needed int(mem_total / 4) in jinja. I solved it by making it two operations:
{% set LS_HEAP_SIZE = grains['mem_total'] / 4 %}
{% set LS_HEAP_SIZE = LS_HEAP_SIZE | round | int %}
Try this
{{ (deet.value*100)|round(1) }}
If we didn’t put parenthesis, round will do only to 100 not to the result.
If filter operator has precedence, then you should use round(3) instead of round(1) in “{{ 100*deet.value|round(1) }}”
I have the following code in my template:
data: [{% for deet in deets %} {{ deet.value*100|round(1) }}{% if not loop.last %},{% endif %} {% endfor %}]
I am expecting data rounded to 1 decimal place. However, when I view the page or source, this is the output I’m getting:
data: [ 44.2765833818, 44.2765833818, 44.2765833818, 44.2765833818, 44.2765833818, 44.2765833818, 44.2765833818, 44.2765833818, 44.2765833818, 44.2765833818 ]
This is not rounded to 1 decimal place. It runs without a template error or anything, but produces incorrect output. My understanding from the documentation, and even a related stack overflow question, are that my format should work. What am I missing or doing wrong?
Didn’t realize the filter operator had precedence over multiplication!
Following up on bernie’s comment, I switched
{{ deet.value*100|round(1) }}
to
{{ 100*deet.value|round(1) }}
which solved the problem. I agree the processing should happen in the code elsewhere, and that would be better practice.
You can put parens around the value that you want to round. (This works for division as well, contrary to what @sobri wrote.)
{{ (deet.value/100)|round }}
NOTE: round
returns a float
so if you really want the int
you have to pass the value through that filter as well.
{{ (deet.value/100)|round|int }}
I ran across this… needed int(mem_total / 4) in jinja. I solved it by making it two operations:
{% set LS_HEAP_SIZE = grains['mem_total'] / 4 %}
{% set LS_HEAP_SIZE = LS_HEAP_SIZE | round | int %}
Try this
{{ (deet.value*100)|round(1) }}
If we didn’t put parenthesis, round will do only to 100 not to the result.
If filter operator has precedence, then you should use round(3) instead of round(1) in “{{ 100*deet.value|round(1) }}”