Jinja模板 - 将float设置为以逗号分隔的货币格式 [英] Jinja Templates - Format a float as comma-separated currency

查看:163
本文介绍了Jinja模板 - 将float设置为以逗号分隔的货币格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将浮动格式设置为以逗号分隔的货币。例如。 543921.9354 变成 $ 543,921.94 。我在Jinja模板中使用了格式过滤器,这似乎是模仿Python中的运算符而不是Python 格式函数?



如何在Jinja中完成这种格式化?是否可以使用格式过滤器?这是我到目前为止,除了逗号完成一切:

$%。2f| format(543921.9354)

这当然会产生

$ p $ 543921.94

-filtersrel =noreferrer>自定义过滤器。如果你使用的是python 2.7,它可以是这样的:
$ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆