473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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
Jul 18 '05 #1
6 3598
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

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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jouke Langhout | last post by:
Hello all! For quite some time now, I've got the following problem: Access won't close properly when a user closes the application. An ACCESS process stays active and that process can only be...
2
by: Colleyville Alan | last post by:
I am using Access and have embedded the ActiveX control Formula One that came with Office 2000. (ver 3.04). I have created and formatted a spreadsheet and now I want to copy the info with...
4
by: DBQueen | last post by:
I have a subform which is in Continuous Forms view. I have added a button to the bottom of the page to move to the next record using the button wizard (result: DoCmd.GoToRecord , , acNext). I...
4
by: Bradley | last post by:
I have an A2000 database in which I have a continuous form with a tick box. There is also a text box with a conditional format that is based on the expression , if it's true then change the...
1
by: GGerard | last post by:
Hello Is there a way to use a variable in the Conditional Formatting of a Textbox? Example : I want the background of a textbox in a continuous form to change color when the value of...
4
by: hope | last post by:
Hi, How can I format a string field using Data Formatting Expression property in datagrid? For example: format last name from BROWN to Brown. Thanks
7
by: L. Scott M. | last post by:
Have a quick simple question: dim x as string x = "1234567890" ------------------------------------------------------- VB 6 dim y as string
8
by: gopal | last post by:
Hi I am trying to write to log file but the formatting of string is not properly aligned in log file result The results looks some thing like this OK move.xml Successful OK ...
8
by: Typehigh | last post by:
I have many text fields with conditional formatting applied, specifically when the condition is "Field Has Focus". Without any events associated with the fields the conditional formatting works...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.