cross platform use of set locale

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Timothy Smith

    #1

    cross platform use of set locale

    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.
  • Marc 'BlackJack' Rintsch

    #2
    Re: cross platform use of set locale

    In <mailman.746.11 11534956.1799.p ython-list@python.org >, Timothy Smith
    wrote:
    [color=blue]
    > 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.[/color]

    Have you tried this::

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

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

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • Timothy Smith

      #3
      Re: cross platform use of set locale

      Marc 'BlackJack' Rintsch wrote:
      [color=blue]
      >In <mailman.746.11 11534956.1799.p ython-list@python.org >, Timothy Smith
      >wrote:
      >
      >
      >[color=green]
      >>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.
      >>
      >>[/color]
      >
      >Have you tried this::
      >
      > import locale
      > locale
      > locale.setlocal e(LC_ALL, '')
      > locale.format(' %.2f', 1000000, True)
      >
      >The `True` turns on grouping. Should work cross platform.
      >
      >Ciao,
      > Marc 'BlackJack' Rintsch
      >
      >[/color]
      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

      Comment

      • Marc 'BlackJack' Rintsch

        #4
        Re: cross platform use of set locale

        In <mailman.751.11 11537997.1799.p ython-list@python.org >, Timothy Smith
        wrote:[color=blue][color=green][color=darkred]
        >>> hi there i need to set my locale so that when i output a number it's
        >>> formatted with thousands grouped. […][/color]
        >>
        >> import locale
        >> locale
        >> locale.setlocal e(LC_ALL, '')
        >> locale.format(' %.2f', 1000000, True)
        >>
        >>The `True` turns on grouping. Should work cross platform.[/color]
        >
        > 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[/color]

        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

        Comment

        • Serge Orlov

          #5
          Re: cross platform use of set locale

          Timothy Smith wrote:[color=blue]
          > 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[/color]

          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.

          Comment

          • Timothy Smith

            #6
            Re: cross platform use of set locale

            Serge Orlov wrote:
            [color=blue]
            >Timothy Smith wrote:
            >
            >[color=green]
            >>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
            >>
            >>[/color]
            >
            >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.
            >
            >
            >[/color]
            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?

            Comment

            • Serge Orlov

              #7
              Re: cross platform use of set locale

              Timothy Smith wrote:[color=blue]
              > Serge Orlov wrote:
              >[color=green]
              > >Timothy Smith wrote:
              > >
              > >[color=darkred]
              > >>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
              > >>
              > >>[/color]
              > >
              > >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.
              > >
              > >
              > >[/color]
              > thats exactly what i'm trying to do, only having to do that for all
              > my outputs is more work then i'd like :/[/color]

              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)




              [color=blue]
              > why is this a misuse of locale? it's exactly what locale is meant for
              > isn't it?[/color]

              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.

              Comment

              • Scott David Daniels

                #8
                Re: cross platform use of set locale

                Timothy Smith wrote:[color=blue]
                > Serge Orlov wrote: ...[color=green]
                >> d = Dollars(1000000 .01)
                >> print "You have %s in your account" % d
                >> and get
                >> You have $1,000,000.01 in your account.[/color][/color]
                [color=blue]
                > 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?[/color]

                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
                Scott.Daniels@A cm.Org

                Comment

                • Damjan

                  #9
                  Re: cross platform use of set locale

                  > SUS has added numeric grouping[color=blue]
                  >
                  > 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.[/color]

                  Hmm, this C code on my system (mk_MK locale)
                  #include <locale.h>
                  #include <stdio.h>
                  int main (void) {
                  setlocale(LC_AL L,"");
                  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.setlocal e(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

                  Comment

                  • Timothy Smith

                    #10
                    Re: cross platform use of set locale

                    Serge Orlov wrote:
                    [color=blue]
                    >Timothy Smith wrote:
                    >
                    >[color=green]
                    >>Serge Orlov wrote:
                    >>
                    >>
                    >>[color=darkred]
                    >>>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.
                    >>>
                    >>>
                    >>>
                    >>>
                    >>>[/color]
                    >>thats exactly what i'm trying to do, only having to do that for all
                    >>my outputs is more work then i'd like :/
                    >>
                    >>[/color]
                    >
                    >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)
                    >
                    >
                    >
                    >
                    >
                    >
                    >[color=green]
                    >>why is this a misuse of locale? it's exactly what locale is meant for
                    >>isn't it?
                    >>
                    >>[/color]
                    >
                    >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.
                    >
                    >
                    >[/color]
                    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.

                    Comment

                    • Facundo Batista

                      #11
                      Re: cross platform use of set locale

                      On Wed, 23 Mar 2005 20:52:06 +1000, Timothy Smith
                      <timothy@open-networks.net> wrote:
                      [color=blue]
                      > Serge Orlov wrote:
                      >[color=green]
                      > >Are you trying to format money? Then you need a special class so that
                      > >you can say:[/color]
                      >
                      > thats exactly what i'm trying to do, only having to do that for all my[/color]

                      If you're dealing with money, two things:

                      a) Use Decimal
                      b) Format it with Raymond's recipe at



                      .. Facundo

                      Blog: http://www.taniquetil.com.ar/plog/
                      PyAr: http://pyar.decode.com.ar/

                      Comment

                      Working...