LC_MONETARY formatting

Martin v. Loewis martin at v.loewis.de
Sat Jan 10 00:27:26 CET 2004


Colin Fox wrote:

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

No.

> Or did
> they provide all that formatting information just so that I have to write
> it myself?

Yes. Use locale.localeconv() to find the relevant parameters.

Make sure you understand that using 'currency_symbol' 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




More information about the Python-list mailing list