简体   繁体   English

Jinja映射/格式的十进制值

[英]Jinja Map/Format Decimal Values

I'm having trouble formatting a list of values in Jinja. 我在Jinja中格式化值列表时遇到麻烦。

Current List: 当前列表:

[0, 0.2608695652173913, 0]

Needed List: 所需清单:

[0, 26.08, 0]

Code: 码:

[{{ record['result']|map(attribute='record')|join(', ') }}]

What is the correct syntax to apply the format filter with something like {0:0.2f} ? format过滤器应用于类似{0:0.2f}的正确语法是什么?

You can do something like this... 你可以做这样的事情...

def FormatDecimal(value):
    return "{0:0.2f}".format(float(value))

jinja2.filters.FILTERS['FormatDecimal'] = FormatDecimal

Then use this in your template... 然后在您的模板中使用它...

{{ SomeValue | FormatDecimal }}

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM