views:

302

answers:

2

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.

+1  A: 

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
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
+5  A: 

You can use the intcomma filter, but I don't think it will work on Decimal objects and it works on Decimal objects. You'll have to convert to float/string first.

sykora
intcomma works just fine with Decimal objects.
Alex Gaynor
Yes it did work with the Decimal object.Thanks.
mhost
I didn't know it worked on Decimal objects. Updated the answer.
sykora