By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,864 Members | 1,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,864 IT Pros & Developers. It's quick & easy.

LC_MONETARY formatting

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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 nave 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

Jul 18 '05 #2

P: n/a
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!

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.

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. 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
Jul 18 '05 #3

P: n/a
this

http://oss.software.ibm.com/icu/

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.
Jul 18 '05 #4

P: n/a

"Colin Fox" <cf**@cfconsulting.ca> wrote in message news:pa****************************@cfconsulting.c a...
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.
Jul 18 '05 #5

P: n/a
Colin Fox wrote:
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!


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

Jul 18 '05 #6

P: n/a
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
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.