cross platform use of set locale

Damjan gdamjan at gmail.com
Wed Mar 23 09:45:01 EST 2005


> 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



More information about the Python-list mailing list