Rounding a decimal in Jinja template Flask


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:


it should be rounded to 666.67

Is there a way I can achieve this?


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:

  {{ ((spend[0] | float) / (user[0] | float)) | round(2) }}

Answered By – β.εηοιτ.βε

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published