我正在尝试将浮点数格式化为逗号分隔的货币。例如543921.9354
变成$543,921.94
. 我format
在 Jinja 模板中使用过滤器,它似乎模仿 Python 中的%
运算符而不是 Pythonformat
函数?
如何在 Jinja 中完成这种格式化?是否可以使用format
过滤器?到目前为止,这就是我所拥有的,它完成了除逗号之外的所有操作:
"$%.2f"|format(543921.9354)
这当然会产生
$543921.94
Update: Using Jinja2 and Python 3, this worked quite nicely in the template without having to define any custom code:
{{ "${:,.2f}".format(543921.9354) }}
I'm not sure exactly what the dependencies are to have this work, but IMHO anyone else reading this answer would do well to at least try it before worrying about custom filters.
为此编写一个自定义过滤器。如果您使用的是 python 2.7,它可能如下所示:
def format_currency(value):
return "${:,.2f}".format(value)
def numberFormat(value):
return format(int(value), ',d')
@app.template_filter()
def numberFormat(value):
return format(int(value), ',d')
@app.app_template_filter()
def numberFormat(value):
return format(int(value), ',d')
{{ '1234567' | numberFormat }}
#prints 1,234,567
{{ format('1234567', ',d') }}
#prints 1,234,567
To extend @alex vasi's answer, I would definitely write a custom filter, but I'd also use python's own locale
functionality, which handles currency grouping, and the symbol,
def format_currency(value):
return locale.currency(value, symbol=True, grouping=True)
The main thing to take note of using locale
is that it doesn't work with the default 'C' locale, so you have to set it so something that's available on your machine.
For what you're looking for, you probably need,
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
but if you wanted sterling pounds, you'd use,
locale.setlocale(locale.LC_ALL, 'en_GB.UTF_8')
.
import locale
locale.setlocale(locale.LC_ALL, 'en_US')
locale.currency(543921.94, symbol=True, grouping=True)
> '$543,921.94'
locale.setlocale(locale.LC_ALL, 'en_GB')
> '£543,921.94'
如果您有 Python 2.6 或更高版本:
您可以出于一个目的编写自定义过滤器,但作为更广泛的解决方案,您还可以更新格式过滤器本身:
from jinja import Environment, FileSystemLoader
def format(fmt_str, *args, **kwargs):
if args and kwargs:
raise jinja2.exceptions.FilterArgumentError(
"can't handle positional and keyword "
"arguments at the same time"
)
ufmt_str = jinja2.utils.soft_unicode(fmt_str)
if kwargs:
return ufmt_str.format(**kwargs)
return ufmt_str.format(*args)
env = Environment(loader=FileSystemLoader('./my/template/dir'))
env.filters.update({
'format': format,
})
This will replace the existing format
filter (as of Jinja 2.7.1). The majority of the function was ripped straight from the format
source. The only difference between this function and jinja's is that it uses the str.format()
function to format the string.
Seeing that Jinja2 (at the time of this writing) no longer supports Python 2.5, I bet it won't be long before the format
filter uses Python's str.format()
.
Custom Filter using Babel (Can be used to format other currencies as well)
Install Babel (http://babel.pocoo.org/en/latest/api/numbers.html)
pip install Babel
Custom Jinja Filter
from babel.numbers import format_currency
@app.template_filter()
def usdollar(value):
return format_currency(value, 'USD', locale='en_US')
app.jinja_env.filters['usdollar'] = usdollar
Usage in Jinja Template:
{{ '-10000.500' | usdollar }}
Output : -$10,000.50