Format numbers in django templates

前端 未结 13 1068
清歌不尽
清歌不尽 2020-11-29 17:22

I\'m trying to format numbers. Examples:

1     => 1
12    => 12
123   => 123
1234  => 1,234
12345 => 12,345

It strikes as a

相关标签:
13条回答
  • 2020-11-29 18:26

    Based on muhuk answer I did this simple tag encapsulating python string.format method.

    • Create a templatetags at your's application folder.
    • Create a format.py file on it.
    • Add this to it:

      from django import template
      
      register = template.Library()
      
      @register.filter(name='format')
      def format(value, fmt):
          return fmt.format(value)
      
    • Load it in your template {% load format %}
    • Use it. {{ some_value|format:"{:0.2f}" }}
    0 讨论(0)
提交回复
热议问题