LC_MONETARY formatting

Colin Fox cfox at cfconsulting.ca
Sat Jan 10 16:15:10 EST 2004


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


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

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,True,locale.LC_MONETARY)
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



More information about the Python-list mailing list