cross platform use of set locale

Serge Orlov Serge.Orlov at gmail.com
Wed Mar 23 13:50:02 CET 2005


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.




More information about the Python-list mailing list