kurtgn
kurtgn

Reputation: 8710

django templates - space as thousand separator

I need to make my numbers have space as thousand separator and dot as decimal separator, i.e. 100 000.00, 2 123 324.32.

I have these settings:

USE_L10N = False
DECIMAL_SEPARATOR = '.'
USE_THOUSAND_SEPARATOR = True
THOUSAND_SEPARATOR = ' '
NUMBER_GROUPING = 3

this helps with decimal separator, but doesn't help with thousand separator. I still have numbers like 100000.00. What am I missing?

Upvotes: 5

Views: 4294

Answers (3)

If you dont need int type to be return:

in view:

@register.filter(name='del_comma')
def del_comma(value):
    if value > 999:
        return str(value // 1000) + ' ' + str(value)[len(str(value // 1000)):]
    return value

in template:

{{ vacancy.salary_from|del_comma }}

Upvotes: 0

Josh
Josh

Reputation: 2478

You can use the built in django.contrib humanize to make your numbers more readable

https://docs.djangoproject.com/en/1.11/ref/contrib/humanize/

In this case intcomma

Upvotes: 0

Selcuk
Selcuk

Reputation: 59297

You should also set USE_L10N to True:

https://docs.djangoproject.com/en/1.7/ref/settings/#std:setting-USE_THOUSAND_SEPARATOR

When USE_L10N is set to True and if this is also set to True, Django will use the values of THOUSAND_SEPARATOR and NUMBER_GROUPING to format numbers.

Upvotes: 3

Related Questions