cross platform use of set locale

T

Timothy Smith

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.
 
M

Marc 'BlackJack' Rintsch

Timothy Smith said:
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
 
T

Timothy Smith

Marc said:
In <[email protected]>, Timothy Smith
wrote:




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
 
M

Marc 'BlackJack' Rintsch

Timothy Smith said:
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
 
S

Serge Orlov

Timothy said:
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.
 
T

Timothy Smith

Serge said:
Timothy Smith wrote:



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?
 
S

Serge Orlov

Timothy said:
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.
 
S

Scott David Daniels

Timothy said:
Serge Orlov wrote: ...
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
(e-mail address removed)
 
D

Damjan

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
 
T

Timothy Smith

Serge said:
Timothy Smith wrote:



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)








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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top