I need to format decimal numbers in jinja2.
When I need to format dates, I call the strftime() method in my template, like this:
{{ somedate.strftime('%Y-%m-%d') }}
I wonder if there is a similar approach to do this over numbers.
Thanks in advance!
A Jinja template doesn't need to have a specific extension: . html , . xml , or any other extension is just fine. A template contains variables and/or expressions, which get replaced with values when a template is rendered; and tags, which control the logic of the template.
You can do it simply like this, the Python way:
{{ '%04d' % 42 }} {{ 'Number: %d' % variable }}
Or using that method:
{{ '%d' | format(42) }}
I personally prefer the first one since it's exactly like in Python.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With