Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a direct approach to format numbers in jinja2?

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!

like image 992
Lucas Avatar asked Oct 01 '12 21:10

Lucas


People also ask

How are variables in Jinja2 templates specified?

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.


1 Answers

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.

like image 80
Lipis Avatar answered Sep 22 '22 18:09

Lipis