Jinja模板 - 将float设置为以逗号分隔的货币格式 [英] Jinja Templates - Format a float as comma-separated currency
本文介绍了Jinja模板 - 将float设置为以逗号分隔的货币格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
543921.9354
变成 $ 543,921.94
。我在Jinja模板中使用了格式
过滤器,这似乎是模仿Python中的%
运算符而不是Python 格式
函数? 如何在Jinja中完成这种格式化?是否可以使用格式
过滤器?这是我到目前为止,除了逗号完成一切:
$%。2f| format(543921.9354)
这当然会产生
$ p $ 543921.94 $ c $
$ b $ $ $ $ $ $ $ def格式化数值
return $ {:,。2f}。格式(值)
I'm trying to format a float as comma-separated currency. E.g. 543921.9354
becomes $543,921.94
. I'm using the format
filter in Jinja templates, which seems to mimic the %
operator in Python rather than the Python format
function?
How can I accomplish this formatting in Jinja? Is it possible using the format
filter? This is what I have so far, which accomplishes everything except the commas:
"$%.2f"|format(543921.9354)
which of course yields
$543921.94
解决方案
Write a custom filter for that. If you are using python 2.7, it can look like this:
def format_currency(value):
return "${:,.2f}".format(value)
这篇关于Jinja模板 - 将float设置为以逗号分隔的货币格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文