Jinja Templates - Format a float as comma-separated currency

后端 未结 6 1715
轻奢々
轻奢々 2021-01-30 16:21

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 t

6条回答
  •  无人及你
    2021-01-30 16:49

    Python3.6:

    def numberFormat(value):
        return format(int(value), ',d')
    

    Flask global filter

    @app.template_filter()
    def numberFormat(value):
        return format(int(value), ',d')
    

    Flask global filter for Blueprint

    @app.app_template_filter()
    def numberFormat(value):
        return format(int(value), ',d')
    

    Call this global filter

    {{ '1234567' | numberFormat }}
    #prints 1,234,567
    

    Calling it in Jinja without assigning a global filter

    {{ format('1234567', ',d') }}
    #prints 1,234,567
    

提交回复
热议问题