Sunday, June 27, 2010

django localization

After digging into django source code, I found a way to format a number or into a proper localization format.

Here is how to do it.

>>> from django.utils import formats
>>> from decimal import Decimal
>>> formats.localize(Decimal(12342341))
u'12,342,341'

You can check out the source code of django.utils.formats, and you can also find some other functions to localize date/time format, and also to sanitize numbers which are localized.

So, remember the DRY principle, and make sure you don't reinvent the wheel by creating existing functionalities.

I'm enjoying django more and more.

No comments:

Post a Comment