在 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屋!
查看全文