23

My model is returning a Decimal(1234567.50), I can't seem to display the Decimal with a thousands separator. Does Django have a way to do this? Do I need to create my own template filter?

Thanks.

mhost
  • 6,930
  • 5
  • 38
  • 45

4 Answers4

36

You can use the intcomma filter.

Kye
  • 4,279
  • 3
  • 21
  • 49
sykora
  • 96,888
  • 11
  • 64
  • 71
  • I didn't know it worked on Decimal objects. Updated the answer. – sykora Feb 16 '10 at 04:01
  • 2
    Don't forget you need to `{% load humanize %}` to be able to use `intcomma`. Also you can combine this with `floatformat` to change how many decimal places are shown. – gdvalderrama Jun 08 '18 at 12:00
  • You can even combine `intcomma` wih `floatformat`, getting the benefit of both (`intcomma` for thousand separator, `floatformat` for minimal or no dot separator). – caram Dec 06 '19 at 13:58
  • In order to use {% load humanize %} you have to activate it as INSTALLED_APPS, see https://docs.djangoproject.com/en/3.0/ref/contrib/humanize/ – Ralf Zosel Jun 13 '20 at 12:22
6

Add humanize to the installed apps in settings.py and load it in the template.

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

One easy way is to import locale and do the formatting in your view function.

Even easier to read this: http://docs.djangoproject.com/en/dev/topics/i18n/#overview

For format localization, it’s just necessary to set USE_L10N = True in your settings file. If USE_L10N is set to True, Django will display numbers and dates in the format of the current locale. That includes field representation on templates, and allowed input formats on the admin.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • 6
    Django 1.2 now has a USE_THOUSAND_SEPARATOR variable you can set in settings.py. See http://docs.djangoproject.com/en/dev/ref/settings/?from=olddocs#use-thousand-separator . This approach was much easier than using the intcomma filter. Once I set both USE_L10N and USE_THOUSAND_SEPARATOR to True, the numbers I wanted to show with thousands separators just started working auto-magically, no template editing necessary. Update: this method seems to work with ints but not longs :( – Eddified Sep 03 '10 at 15:37
  • What happen when `USE_L10N` doesn't work like here http://stackoverflow.com/questions/7918523/django-localisation – Robert Johnstone Oct 31 '11 at 15:08
  • 5
    USE_THOUSAND_SEPARATOR is evil because it converts other values. e.g. when using filebrowser, image.width will be formatted. When using thousands with dots, this will lead to width="1.200" height="800"... 1.2 instead of 1200 – Alexei Martchenko Jun 03 '19 at 13:55
  • What is the solution for this ? How to use thousand operator only to specific number fileds? – Chetna rustagi Feb 27 '20 at 13:11
4

You could add the following to settings.py:

USE_THOUSAND_SEPARATOR = True

That should work and you don't have to act with loading stuff. https://docs.djangoproject.com/en/4.1/ref/settings/#use-thousand-separator

Abdul Aziz Barkat
  • 19,475
  • 3
  • 20
  • 33
Gunnar
  • 41
  • 1
  • Caution: this setting can be evil - I broke a site by setting this to true because values in templates with scripts, dynamic widths, etc. were being rendered with commas, which completely broke a pile of stuff. Use with care!! – powderflask Nov 01 '22 at 18:18