locale aware number formats

317 views
Skip to first unread message

pihentagy

unread,
Sep 23, 2008, 6:21:40 AM9/23/08
to Django users
Hi all!

How can I print a number using the locale's thousand separator and
decimal?

Basically I hoped ther is a template filter, but floatformat doesn't
have thousand separator, and seems to use decimal point always.

thanks
Gergo

Jarek Zgoda

unread,
Sep 23, 2008, 6:28:34 AM9/23/08
to django...@googlegroups.com
Wiadomość napisana w dniu 2008-09-23, o godz. 12:21, przez pihentagy:


Take a look at Babel package and Django helpers - http://babel.edgewall.org/

--
We read Knuth so you don't have to. - Tim Peters

Jarek Zgoda, R&D, Redefine
jarek...@redefine.pl

pihentagy

unread,
Sep 23, 2008, 11:51:22 AM9/23/08
to Django users
On Sep 23, 12:28 pm, Jarek Zgoda <jarek.zg...@redefine.pl> wrote:
> Wiadomość napisana w dniu 2008-09-23, o godz. 12:21, przez pihentagy:
>
> > How can I print a number using the locale's thousand separator and
> > decimal?
>
> > Basically I hoped ther is a template filter, but floatformat doesn't
> > have thousand separator, and seems to use decimal point always.
>
> Take a look at Babel package and Django helpers -http://babel.edgewall.org/

Ok, but there is also the module locale.
Should I really use other libraries for just displaying a number in a
page? Django supports translations, doesn't it supports printing
numbers and currency honoring the currently selected language?

thanks
Gergo

Karen Tracey

unread,
Sep 23, 2008, 12:01:29 PM9/23/08
to django...@googlegroups.com
2008/9/23 pihentagy <pihe...@gmail.com>

Malcolm Tredinnick

unread,
Sep 23, 2008, 11:19:26 PM9/23/08
to django...@googlegroups.com

We can't use Python's locale module in Django. It doesn't allow
different locale settings in different threads.

Prior to 1.1 there will be some changes made there to add support for
number and date printing and validation (Karen's already pointed to one
of the relevant tickets). Currency display is probably out of scope,
since that's not actually particularly locale-dependent: it's specific
to the currency itself, not the output locale (display USD 1.25 as JPY
125,000 or something is just wrong).

Regards,
Malcolm


Jarek Zgoda

unread,
Sep 24, 2008, 2:51:13 AM9/24/08
to django...@googlegroups.com
Wiadomość napisana w dniu 2008-09-23, o godz. 17:51, przez pihentagy:

>>> How can I print a number using the locale's thousand separator and
>>> decimal?
>>
>>> Basically I hoped ther is a template filter, but floatformat doesn't
>>> have thousand separator, and seems to use decimal point always.
>>
>> Take a look at Babel package and Django helpers -http://babel.edgewall.org/
>
> Ok, but there is also the module locale.
> Should I really use other libraries for just displaying a number in a
> page? Django supports translations, doesn't it supports printing
> numbers and currency honoring the currently selected language?

Additionally to what's been said by others, Python's own locale module
isn't that good in formatting dates and numbers. Babel uses CLDR
database to provide most accurate l10n data for many locations and
languages, event those not supported by standard NLS machinery. Plus
on linux it does not require installing appropriate language support -
you can do eg. Hungarian date, number and currency formatting without
installing Hungarian language support package (language-pack-hu on
Ubuntu).

Reply all
Reply to author
Forward
0 new messages