I would like to round up a decimal value as a result of a division in Jinja template is there a way I can do this?
I have tried doing it like this:
<td>{{'%0.2f'|format(spend[0] / user[0] |float ) }}</td>
but I got an error:
TypeError: unsupported operand type(s) for /: 'decimal.Decimal' and 'float'
if the result of the division is for example:
666.6666666666666666666666667
it should be rounded to 666.67
Is there a way I can achieve this?
CodePudding user response:
You do have two issues:
- You seems to have mixed types in your variables (one decimal and one float), you should cast them both to float, prior to the division (as there is no decimal type in Jinja)
- You are using a format filter when you want to round, there is, though, a
round
filter for that purpose
Given those two remarks, your snippet of code should be:
<td>
{{ ((spend[0] | float) / (user[0] | float)) | round(2) }}
</td>