LC_MONETARY formatting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Colin Fox

    LC_MONETARY formatting

    Hi, all.

    This feels like a stupid question to me, but I've scoured the Internet
    (yes, the whole thing! :) and I can't find a single example of using the
    locale module to format money.

    So I have:

    cash = 12345.6

    and the locale is set to "en_CA" (though that doesn't matter, really).

    I would like to be able to print "$12,345.60 "

    Is there a format string, or function, or *something* that will take
    advantage of the monetary formatting info in the locale object? Or did
    they provide all that formatting information just so that I have to write
    it myself?

    The only docs on using the locale object contain a single example of
    comparing two strings. It would sure be nice to see examples of each of
    the locale features.

    Any help would be GREATLY appreciated!

    Thanks,
    cf
  • Martin v. Loewis

    #2
    Re: LC_MONETARY formatting

    Colin Fox wrote:
    [color=blue]
    > So I have:
    >
    > cash = 12345.6
    >
    > and the locale is set to "en_CA" (though that doesn't matter, really).
    >
    > I would like to be able to print "$12,345.60 "
    >
    > Is there a format string, or function, or *something* that will take
    > advantage of the monetary formatting info in the locale object?[/color]

    No.
    [color=blue]
    > Or did
    > they provide all that formatting information just so that I have to write
    > it myself?[/color]

    Yes. Use locale.localeco nv() to find the relevant parameters.

    Make sure you understand that using 'currency_symbo l' presents a problem
    if the amount you have is already in some currency. Printing the same
    value in different locales using the naïve formatting algorithm is
    likely to produce incorrect results, as you have to apply some exchange
    rate, which varies from day to day, and from trading place to trading
    place.

    IOW, LC_MONETARY is useless for financial applications - if the amount
    is in ¤, using the locale's currency symbol would be wrong.

    Regards,
    Martin

    Comment

    • Colin Fox

      #3
      Re: LC_MONETARY formatting

      On Sat, 10 Jan 2004 00:27:26 +0100, Martin v. Loewis wrote:
      [color=blue]
      > Colin Fox wrote:[/color]
      <..>[color=blue][color=green]
      >> I would like to be able to print "$12,345.60 "
      >>
      >> Is there a format string, or function, or *something* that will take
      >> advantage of the monetary formatting info in the locale object?[/color]
      >
      > No.[/color]

      I can't believe that I'm the first person who ever wanted to print a
      number as a currency value. Why go to the great lengths of what's provided
      in localeconv(), only to leave it up to every single programmer to
      re-implement the solution? What a waste of time and effort!

      I can't see any reason not to provide a monetary converter within locale,
      particularly when it's obviously got all the info it needs.
      [color=blue]
      >
      > IOW, LC_MONETARY is useless for financial applications - if the amount
      > is in ¤, using the locale's currency symbol would be wrong.[/color]

      That's true whether you use LC_MONETARY or not. Unless you know the units
      your number is in, you can't assign a symbol to it. In my case, I have
      numbers that are always in either Canadian or US dollars, so the dollar
      sign is fine, and the thousands-separator value is fine.

      cf

      Comment

      • Frank Bechmann

        #4
        Re: LC_MONETARY formatting

        this



        is - as far as I know - *the* OS catch-all project for
        encoding-conversion and localisation tasks. It's written in C++, has a
        C wrapper and Java's functionality on these topics is based on ICU. I
        don't know whether there's a python wrapper for it, but IIRC the
        gnue-project (which is mainly written in Python) uses ICU.

        but as stated above often in commercial applications the problem is
        not the currency formatting but to provide the complete information of
        amount, source currency and target currency throughout the complete
        flow of the program.

        Comment

        • Serge Orlov

          #5
          Re: LC_MONETARY formatting


          "Colin Fox" <cfox@cfconsult ing.ca> wrote in message news:pan.2004.0 1.10.00.01.07.9 62349@cfconsult ing.ca...[color=blue]
          > On Sat, 10 Jan 2004 00:27:26 +0100, Martin v. Loewis wrote:
          >[color=green]
          > > Colin Fox wrote:[/color]
          > <..>[color=green][color=darkred]
          > >> I would like to be able to print "$12,345.60 "
          > >>
          > >> Is there a format string, or function, or *something* that will take
          > >> advantage of the monetary formatting info in the locale object?[/color]
          > >
          > > No.[/color]
          >
          > I can't believe that I'm the first person who ever wanted to print a
          > number as a currency value. Why go to the great lengths of what's provided
          > in localeconv(), only to leave it up to every single programmer to
          > re-implement the solution? What a waste of time and effort![/color]

          Compared to the effort of all humanity to move forward the waste
          of time and effort is dwarf <wink>
          [color=blue]
          >
          > I can't see any reason not to provide a monetary converter within locale,
          > particularly when it's obviously got all the info it needs.[/color]

          It's not as simple as you think. The proper way to do it is to have locale
          aware money type.
          [color=blue]
          >[color=green]
          > >
          > > IOW, LC_MONETARY is useless for financial applications - if the amount
          > > is in ¤, using the locale's currency symbol would be wrong.[/color]
          >
          > That's true whether you use LC_MONETARY or not. Unless you know the units
          > your number is in, you can't assign a symbol to it.[/color]

          That's why LC_MONETARY (strfmon) is broken. Have you read strfmon manual?
          [color=blue]
          > In my case, I have
          > numbers that are always in either Canadian or US dollars, so the dollar
          > sign is fine, and the thousands-separator value is fine.[/color]

          You should have money class. It should be a subclass of FixedPoint class.
          If you want to deal with Canadian or US dollars only it's as simple as:
          class Dollars(FixedPo int):
          def __init__(self,a mount):
          super(Dollars,s elf).__init__(a mount)
          def __str__(self):
          if self < 0:
          return locale.format("-$%.2f",-self,True)
          else:
          return locale.format(" $%.2f",self,Tru e)


          No warranty, of course, that it works and fulfills all your needs.[color=blue][color=green][color=darkred]
          >>> locale.format(" $%.2f",10000000 00000,True)[/color][/color][/color]
          '$1,000,000,000 ,000.00'

          -- Serge.


          Comment

          • Martin v. Löwis

            #6
            Re: LC_MONETARY formatting

            Colin Fox wrote:
            [color=blue]
            > I can't believe that I'm the first person who ever wanted to print a
            > number as a currency value. Why go to the great lengths of what's provided
            > in localeconv(), only to leave it up to every single programmer to
            > re-implement the solution? What a waste of time and effort![/color]

            As Serge points out, C has strfmon. For Python, better start believing
            that you are the first person who ever wanted to print a number as a
            currency value in a locale-dependent way.

            Feel free to contribute patches if you think this should be provided
            out of the box.

            Regards,
            Martin

            Comment

            • Colin Fox

              #7
              Re: LC_MONETARY formatting

              On Sat, 10 Jan 2004 16:12:10 +0300, Serge Orlov wrote:

              [color=blue][color=green]
              >> In my case, I have
              >> numbers that are always in either Canadian or US dollars, so the dollar
              >> sign is fine, and the thousands-separator value is fine.[/color]
              >
              > You should have money class. It should be a subclass of FixedPoint class.
              > If you want to deal with Canadian or US dollars only it's as simple as:
              > class Dollars(FixedPo int):
              > def __init__(self,a mount):
              > super(Dollars,s elf).__init__(a mount)
              > def __str__(self):
              > if self < 0:
              > return locale.format("-$%.2f",-self,True)
              > else:
              > return locale.format(" $%.2f",self,Tru e)
              >
              >
              > No warranty, of course, that it works and fulfills all your needs.[color=green][color=darkred]
              >>>> locale.format(" $%.2f",10000000 00000,True)[/color][/color]
              > '$1,000,000,000 ,000.00'[/color]

              Thanks, the locale.format() function indeed does what I need. However, my
              understanding of it is that it uses the LC_NUMERIC locale info, rather
              than the LC_MONETARY. For my purposes, this is certainly sufficient.

              I'd like to use a class, but since this is part of a Zope application,
              that's a little difficult (and overkill for this particular need).

              It would be nice if we could have something like:
              locale.format(" %m",1000000,Tru e,locale.LC_MON ETARY)
              though as is indicated in the strfmon docs, it wouldn't be quite so simple
              (taking into account the different types of currency markers, padding,
              spacing, etc).

              cf

              Comment

              Working...