[Django]-How to format a number in django and respect localization

2👍

If this number format is your localized format, there’s a builtin filter that does what I believe your filter is trying to accomplish: https://docs.djangoproject.com/en/2.0/topics/i18n/formatting/#template-filters

If that’s not your speed, and you want something a bit more customized, you can take the localization call from the source code of that filter and customize it for your needs:

from django.utils import formats
@register.filter
def format_number(value):
    l10n_num = formats.localize(value, use_l10n=True)
    # Do something else here
    return l10n_num

Hope this can help you out!

Leave a comment