I wrote a custom filter like this which calls this method in a util file:
def util_format_number(value):
return "{:,.1f}".format(value)
The idea is to only show one decimal place...but when used inside a template it won't give me a localized number like I have configured in settings:
LANGUAGE_CODE = 'es-es'
USE_I18N = True
USE_L10N = True
USE_THOUSAND_SEPARATOR = True
DECIMAL_SEPARATOR = ','
THOUSAND_SEPARATOR = '.'
NUMBER_GROUPING = 3
The desired result would be 23.444.523,3
My actual filter looks like this:
@register.filter
def format_number(value):
return util_format_number(value)
I want to be able to use the formatter not only in templates, but inside view code where I create custom graphs (that I later print in the templates).
Any hints?
Best regards and thanks in advance.
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!
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.