473,385 Members | 1,372 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,385 software developers and data experts.

Format a number as currency! I can't find any help on this simpleproblem.

I am relatively new to Python. Love it, but I find things that I can do
easily in .NET and cannot find a way to do in Python. I need to format a
number as currency, for example 12343.56 to $12,343.56.

In C# all I need to do is decimal x = 12343.56 then x.ToString("$###,###.00");

I cannot find a way to do this in Python. I must be missing something very
simple here. I have tried the locale module but it will not put in the commas.
I hope I do not have to write my own formatting routine...surely one is out
there and I just can't find it.

Running on XP Pro. Python 2.4.3.

Thanks much in advance,

Richard

Oct 11 '06 #1
4 6049
>>import locale
>>locale.setlocale(locale.LC_ALL, 'English_United States.1252')
'English_United States.1252'
>>conv = locale.localeconv()
x = 1234567.8
locale.format("%d", x, grouping=True)
'1,234,567'
>>locale.format("%s%.*f", (conv['currency_symbol'], conv['int_frac_digits'], x), grouping=True)
'$1,234,567.80'

Hi Richards,

This works for me. I agree it's a bit complicated compared to C# but it
works. I'd put it in a function if I had to use it.
Richard Kessler wrote:
I am relatively new to Python. Love it, but I find things that I can do
easily in .NET and cannot find a way to do in Python. I need to format a
number as currency, for example 12343.56 to $12,343.56.

In C# all I need to do is decimal x = 12343.56 then x.ToString("$###,###.00");

I cannot find a way to do this in Python. I must be missing something very
simple here. I have tried the locale module but it will not put in the commas.
I hope I do not have to write my own formatting routine...surely one is out
there and I just can't find it.

Running on XP Pro. Python 2.4.3.

Thanks much in advance,

Richard
Oct 11 '06 #2
jr
Hi, Bernard.
Just tried the first 2 commands on win XP, Python 2.5 under Idle.
An Error is raised: "unsupported locale setting" (lib/locale.py in
setlocale, line 476).
Actually I get the error also under Python 2.4.3
Any idea what I'm missing?
Thanks in advance.
Jürgen
Bernard wrote:
>import locale
locale.setlocale(locale.LC_ALL, 'English_United States.1252')
'English_United States.1252'
>conv = locale.localeconv()
x = 1234567.8
locale.format("%d", x, grouping=True)
'1,234,567'
>locale.format("%s%.*f", (conv['currency_symbol'], conv['int_frac_digits'], x), grouping=True)
'$1,234,567.80'

Hi Richards,

This works for me. I agree it's a bit complicated compared to C# but it
works. I'd put it in a function if I had to use it.
Richard Kessler wrote:
I am relatively new to Python. Love it, but I find things that I can do
easily in .NET and cannot find a way to do in Python. I need to format a
number as currency, for example 12343.56 to $12,343.56.

In C# all I need to do is decimal x = 12343.56 then x.ToString("$###,###.00");

I cannot find a way to do this in Python. I must be missing something very
simple here. I have tried the locale module but it will not put in the commas.
I hope I do not have to write my own formatting routine...surely one isout
there and I just can't find it.

Running on XP Pro. Python 2.4.3.

Thanks much in advance,

Richard
Oct 17 '06 #3
Replace the conv function call with locale.localeconv.

--
Jerry

Oct 17 '06 #4
jr wrote:
>>import locale
locale.setlocale(locale.LC_ALL, 'English_United States.1252')
'English_United States.1252'
Just tried the first 2 commands on win XP, Python 2.5 under Idle.
An Error is raised: "unsupported locale setting" (lib/locale.py in
setlocale, line 476).
Actually I get the error also under Python 2.4.3
Any idea what I'm missing?
Let your machine decide
>>locale.setlocale(locale.LC_ALL, "")
'de_DE.UTF-8'

or try something less specific:
>>locale.setlocale(locale.LC_ALL, "en_US")
'en_US'

Peter
Oct 17 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bill Stanard | last post by:
I have had no success using the format function as follows (the two lines of code run one after the other): 'displays a running total of lblAmtdue.Caption 'contents in txtTotal.Text...
3
by: John H. | last post by:
Hi, Why if set a DataTable in Crystal Report Net Table with method SetDataSource, 'ReportDocument.SetDataSource(oDataTable)', lost the Number and Currency format with the decimal digits and...
3
by: Abby Lee | last post by:
I found the following script somewhere so I can change values to money before the form is printed. The page works for most people but not some. It worked fine for me but I get the yellow tri-angle...
1
by: Newbie | last post by:
Hi all, I am having three queries which I combined using an union query and each query has a cost field. In the report when I try to get the sum() I get a number with many decimal zero's all my...
7
by: zlf | last post by:
1¡¢Here is a number. double nu = 123 2¡¢Format it with 3¡¢The result is "$123" I want to ask how to deformat "$123" to 123 Thx (Currency symbol is not restricted to '$')
3
by: Slonocode | last post by:
I have some textboxes bound to an access db. I wanted to format the textboxes that displayed currency and date info so I did the following: Dim WithEvents oBidAmt As Binding oBidAmt = New...
6
by: John A Grandy | last post by:
i thought the following would work for currency {0:$000,000,000.00} but it is displaying ( for example ) : $000,025,368.87 i don't want the extra zeros ...
5
by: najimou | last post by:
Hi everyone I have a calculated field in a query, somehow, it will not accept the format to be that of Currency and just displays plain number I attempted formatcurrency in code, changed...
2
by: Dhananjay | last post by:
Hi all , I have got problem when i am tring to exportGridview Data into Excel format. It is going into text format ,but what i want is if the field is number/currency then it should go into...
7
by: Nariban Barkan | last post by:
Hi All, I have a decimal value on the GridView BoundField which comes from sql server and it seems on gridview like that; 2096.62 14899.01 I want to display that values like 2.096,62...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.