Formatting Numbers in Jinja

November 05, 2017

Jinja offers some built-in filters that can be used for simple number formatting in templates. Any more complex number formatting should be done outside of the template.

Rounding

Integers can be rounded with the round filter

{{ number|round }}

Make sure to cast floats and strings to integers to properly round them!

{{ number|round|int }}

Basic Currency

Python's string formatting can be used to format currency:

{{ "$%.2f"|format(price) }}

Troubleshooting

Still getting errors? Your value may be coming in as a string, try converting it to a float:

{{ "$%.2f"|format(value|float) }}

Further Reading

Didn't find what you're looking for?

Ask our support team, we're here to help!

Contact Support