在jinja2中有直接格式化数字的方法吗? [英] Is there a direct approach to format numbers in jinja2?
本文介绍了在jinja2中有直接格式化数字的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在jinja2中格式化十进制数字.
I need to format decimal numbers in jinja2.
当我需要格式化日期时,可以在模板中调用strftime()方法,如下所示:
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.
提前谢谢!
推荐答案
您可以使用Python这样简单地做到这一点:
You can do it simply like this, the Python way:
{{ '%04d' % 42 }}
{{ 'Number: %d' % variable }}
或使用该方法:
{{ '%d' | format(42) }}
我个人更喜欢第一个,因为它与Python完全一样.
I personally prefer the first one since it's exactly like in Python.
这篇关于在jinja2中有直接格式化数字的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文