LC_MONETARY formatting

Serge Orlov sombDELETE at pobox.ru
Sat Jan 10 08:12:10 EST 2004


"Colin Fox" <cfox at cfconsulting.ca> wrote in message news:pan.2004.01.10.00.01.07.962349 at cfconsulting.ca...
> On Sat, 10 Jan 2004 00:27:26 +0100, Martin v. Loewis wrote:
>
> > Colin Fox wrote:
> <..>
> >> 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?
> >
> > No.
>
> 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!

Compared to the effort of all humanity to move forward the waste
of time and effort is dwarf <wink>

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

It's not as simple as you think. The proper way to do it is to have locale
aware money type.

>
> >
> > IOW, LC_MONETARY is useless for financial applications - if the amount
> > is in ¤, using the locale's currency symbol would be wrong.
>
> 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.

That's why LC_MONETARY (strfmon) is broken. Have you read strfmon manual?

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

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(FixedPoint):
    def __init__(self,amount):
        super(Dollars,self).__init__(amount)
    def __str__(self):
        if self < 0:
            return locale.format("-$%.2f",-self,True)
        else:
            return locale.format("$%.2f",self,True)


No warranty, of course, that it works and fulfills all your needs.
>>> locale.format("$%.2f",1000000000000,True)
'$1,000,000,000,000.00'

-- Serge.





More information about the Python-list mailing list