By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,002 Members | 1,171 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,002 IT Pros & Developers. It's quick & easy.

cross platform use of set locale

P: n/a
hi there i need to set my locale so that when i output a number it's
formatted with thousands grouped. the problem i've found is there
doesn't seem to be very good cross platform support for locales, even
between unix's it's horrid.
Jul 18 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
In <ma*************************************@python.or g>, Timothy Smith
wrote:
hi there i need to set my locale so that when i output a number it's
formatted with thousands grouped. the problem i've found is there
doesn't seem to be very good cross platform support for locales, even
between unix's it's horrid.


Have you tried this::

import locale
locale
locale.setlocale(LC_ALL, '')
locale.format('%.2f', 1000000, True)

The `True` turns on grouping. Should work cross platform.

Ciao,
Marc 'BlackJack' Rintsch
Jul 18 '05 #2

P: n/a
Marc 'BlackJack' Rintsch wrote:
In <ma*************************************@python.or g>, Timothy Smith
wrote:
hi there i need to set my locale so that when i output a number it's
formatted with thousands grouped. the problem i've found is there
doesn't seem to be very good cross platform support for locales, even
between unix's it's horrid.


Have you tried this::

import locale
locale
locale.setlocale(LC_ALL, '')
locale.format('%.2f', 1000000, True)

The `True` turns on grouping. Should work cross platform.

Ciao,
Marc 'BlackJack' Rintsch

thats ok, but how do i get it to group thousands with a , ?
and thats would mean i'd have to run everything through a formatter
before i displayed it :/ it'd be nicer if i could just select a proper
locale
Jul 18 '05 #3

P: n/a
In <ma*************************************@python.or g>, Timothy Smith
wrote:
hi there i need to set my locale so that when i output a number it's
formatted with thousands grouped. […]


import locale
locale
locale.setlocale(LC_ALL, '')
locale.format('%.2f', 1000000, True)

The `True` turns on grouping. Should work cross platform.


thats ok, but how do i get it to group thousands with a , ? and thats
would mean i'd have to run everything through a formatter before i
displayed it :/ it'd be nicer if i could just select a proper locale


It uses the locale of the system. If *your* systems locale is configured
to use ',' as separator for grouping, then it should be used by
`locale.format()`.

Ciao,
Marc 'BlackJack' Rintsch
Jul 18 '05 #4

P: n/a
Timothy Smith wrote:
thats ok, but how do i get it to group thousands with a , ?
and thats would mean i'd have to run everything through a formatter
before i displayed it :/ it'd be nicer if i could just select a
proper locale


I think you're misusing locale. There is no guarantee that any specific
locale will have properties (like grouping) set to a known value.
Are you trying to format money? Then you need a special class so that
you can say:

d = Dollars(1000000.01)
print "You have %s in your account" % d

and get

You have $1,000,000.01 in your account.

Serge.

Jul 18 '05 #5

P: n/a
Serge Orlov wrote:
Timothy Smith wrote:

thats ok, but how do i get it to group thousands with a , ?
and thats would mean i'd have to run everything through a formatter
before i displayed it :/ it'd be nicer if i could just select a
proper locale


I think you're misusing locale. There is no guarantee that any specific
locale will have properties (like grouping) set to a known value.
Are you trying to format money? Then you need a special class so that
you can say:

d = Dollars(1000000.01)
print "You have %s in your account" % d

and get

You have $1,000,000.01 in your account.

Serge.

thats exactly what i'm trying to do, only having to do that for all my
outputs is more work then i'd like :/
why is this a misuse of locale? it's exactly what locale is meant for
isn't it?
Jul 18 '05 #6

P: n/a
Timothy Smith wrote:
Serge Orlov wrote:
Timothy Smith wrote:

thats ok, but how do i get it to group thousands with a , ?
and thats would mean i'd have to run everything through a formatter
before i displayed it :/ it'd be nicer if i could just select a
proper locale
I think you're misusing locale. There is no guarantee that any
specific locale will have properties (like grouping) set to a known
value. Are you trying to format money? Then you need a special class
so that you can say:

d = Dollars(1000000.01)
print "You have %s in your account" % d

and get

You have $1,000,000.01 in your account.

Serge.

thats exactly what i'm trying to do, only having to do that for all
my outputs is more work then i'd like :/


SUS has added numeric grouping

For some numeric conversions a radix character (`decimal
point') or thousands' grouping character is used. The
actual character used depends on the LC_NUMERIC part of
the locale. The POSIX locale uses `.' as radix character,
and does not have a grouping character. Thus,
printf("%'.2f", 1234567.89);
results in `1234567.89' in the POSIX locale, in
`1234567,89' in the nl_NL locale, and in `1.234.567,89' in
the da_DK locale.

but they hasn't added monetary grouping. I don't think you'll
get monetary grouping anytime soon. Besides as far as I understood
your question, you *always* want grouping, right?

Actually I don't think a cryptic flag is better than an explicit
formatter. What do you think is more clear for a maintainer of your
code?

print "%'.2f" % amount

or

print "%s" % dollars(amount)


why is this a misuse of locale? it's exactly what locale is meant for
isn't it?


I just reacted to your words "select a proper locale" and "how do i get
it to group thousands with a ,". It's just not a good idea to select
a locale and expect the grouping character to be "," or expect
grouping,
see nl_NL locale example above.

Serge.

Jul 18 '05 #7

P: n/a
Timothy Smith wrote:
Serge Orlov wrote: ...
d = Dollars(1000000.01)
print "You have %s in your account" % d
and get
You have $1,000,000.01 in your account.
thats exactly what i'm trying to do, only having to do that for all my
outputs is more work then i'd like :/
why is this a misuse of locale? it's exactly what locale is meant for
isn't it?


locale is so that you can write code that runs several different places
and obeys local conventions on output at each of those places. Not so
much for formatting how you like (which would be easier to write and
control yourself). Just write a function "commad" and use it.

--Scott David Daniels
Sc***********@Acm.Org
Jul 18 '05 #8

P: n/a
> SUS has added numeric grouping

For some numeric conversions a radix character (`decimal
point') or thousands' grouping character is used. The
actual character used depends on the LC_NUMERIC part of
the locale. The POSIX locale uses `.' as radix character,
and does not have a grouping character. Thus,
printf("%'.2f", 1234567.89);
results in `1234567.89' in the POSIX locale, in
`1234567,89' in the nl_NL locale, and in `1.234.567,89' in
the da_DK locale.


Hmm, this C code on my system (mk_MK locale)
#include <locale.h>
#include <stdio.h>
int main (void) {
setlocale(LC_ALL,"");
printf("%'.2f\n", 1234567.89);
return 0;
}
Outputs: 1 234 567,89 as expected.

But this Python program (2.3.4 [GCC 3.3.3] on linux2)
import locale
locale.setlocale(locale.LC_ALL, "")
print locale.format("%'.2f", 1234567.89, grouping=True)

complains about the ' in the format
ValueError: unsupported format character ''' (0x27) at index 1
without the ' it outputs: 1234567,89

--
damjan
Jul 18 '05 #9

P: n/a
Serge Orlov wrote:
Timothy Smith wrote:

Serge Orlov wrote:
Timothy Smith wrote:


thats ok, but how do i get it to group thousands with a , ?
and thats would mean i'd have to run everything through a formatter
before i displayed it :/ it'd be nicer if i could just select a
proper locale


I think you're misusing locale. There is no guarantee that any
specific locale will have properties (like grouping) set to a known
value. Are you trying to format money? Then you need a special class
so that you can say:

d = Dollars(1000000.01)
print "You have %s in your account" % d

and get

You have $1,000,000.01 in your account.

Serge.


thats exactly what i'm trying to do, only having to do that for all
my outputs is more work then i'd like :/


SUS has added numeric grouping

For some numeric conversions a radix character (`decimal
point') or thousands' grouping character is used. The
actual character used depends on the LC_NUMERIC part of
the locale. The POSIX locale uses `.' as radix character,
and does not have a grouping character. Thus,
printf("%'.2f", 1234567.89);
results in `1234567.89' in the POSIX locale, in
`1234567,89' in the nl_NL locale, and in `1.234.567,89' in
the da_DK locale.

but they hasn't added monetary grouping. I don't think you'll
get monetary grouping anytime soon. Besides as far as I understood
your question, you *always* want grouping, right?

Actually I don't think a cryptic flag is better than an explicit
formatter. What do you think is more clear for a maintainer of your
code?

print "%'.2f" % amount

or

print "%s" % dollars(amount)


why is this a misuse of locale? it's exactly what locale is meant for
isn't it?


I just reacted to your words "select a proper locale" and "how do i get
it to group thousands with a ,". It's just not a good idea to select
a locale and expect the grouping character to be "," or expect
grouping,
see nl_NL locale example above.

Serge.

i'm pretty sure english au uses thousands grouping with ,
i've managers to make this work, however it just isn't cross platform at
all.

Jul 18 '05 #10

P: n/a
On Wed, 23 Mar 2005 20:52:06 +1000, Timothy Smith
<ti*****@open-networks.net> wrote:
Serge Orlov wrote:
Are you trying to format money? Then you need a special class so that
you can say:


thats exactly what i'm trying to do, only having to do that for all my


If you're dealing with money, two things:

a) Use Decimal
b) Format it with Raymond's recipe at
http://docs.python.org/lib/decimal-recipes.html
.. Facundo

Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://pyar.decode.com.ar/
Jul 18 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.