Format numbers in django templates

前端 未结 13 1067
清歌不尽
清歌不尽 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:16

    Slightly off topic:

    I found this question while looking for a way to format a number as currency, like so:

    $100
    ($50)  # negative numbers without '-' and in parens
    

    I ended up doing:

    {% if   var >= 0 %} ${{ var|stringformat:"d" }}
    {% elif var <  0 %} $({{ var|stringformat:"d"|cut:"-" }})
    {% endif %}
    

    You could also do, e.g. {{ var|stringformat:"1.2f"|cut:"-" }} to display as $50.00 (with 2 decimal places if that's what you want.

    Perhaps slightly on the hacky side, but maybe someone else will find it useful.

    0 讨论(0)
  • 2020-11-29 18:17

    Be aware that changing locale is process-wide and not thread safe (iow., can have side effects or can affect other code executed within the same process).

    My proposition: check out the Babel package. Some means of integrating with Django templates are available.

    0 讨论(0)
  • 2020-11-29 18:18

    The humanize solution is fine if your website is in English. For other languages, you need another solution: I recommend using Babel. One solution is to create a custom template tag to display numbers properly. Here's how: just create the following file in your_project/your_app/templatetags/sexify.py:

    # -*- coding: utf-8 -*-
    from django import template
    from django.utils.translation import to_locale, get_language
    from babel.numbers import format_number
    
    register = template.Library()
    
    def sexy_number(context, number, locale = None):
        if locale is None:
            locale = to_locale(get_language())
        return format_number(number, locale = locale)
    
    register.simple_tag(takes_context=True)(sexy_number)
    

    Then you can use this template tag in your templates like this:

    {% load sexy_number from sexify %}
    
    {% sexy_number 1234.56 %}
    
    • For an american user (locale en_US) this displays 1,234.56.
    • For a french user (locale fr_FR), this displays 1 234,56.
    • ...

    Of course you can use variables instead:

    {% sexy_number some_variable %}
    

    Note: the context parameter is currently not used in my example, but I put it there to show that you can easily tweak this template tag to make it use anything that's in the template context.

    0 讨论(0)
  • 2020-11-29 18:20

    Django's contributed humanize application does this:

    {% load humanize %}
    {{ my_num|intcomma }}
    

    Be sure to add 'django.contrib.humanize' to your INSTALLED_APPS list in the settings.py file.

    0 讨论(0)
  • 2020-11-29 18:22

    Building on other answers, to extend this to floats, you can do:

    {% load humanize %}
    {{ floatvalue|floatformat:2|intcomma }}
    

    Documentation: floatformat, intcomma.

    0 讨论(0)
  • 2020-11-29 18:25

    In case someone stumbles upon this, in Django 2.0.2 you can use this

    Thousand separator. Be sure to read format localization as well.

    0 讨论(0)
提交回复
热议问题