简体   繁体   中英

Jinja Map/Format Decimal Values

I'm having trouble formatting a list of values in 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} ?

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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