473,569 Members | 2,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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...surel y 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 6059
>>import locale
>>locale.setloc ale(locale.LC_A LL, 'English_United States.1252')
'English_United States.1252'
>>conv = locale.localeco nv()
x = 1234567.8
locale.format ("%d", x, grouping=True)
'1,234,567'
>>locale.format ("%s%.*f", (conv['currency_symbo l'], conv['int_frac_digit s'], 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...surel y 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: "unsupporte d 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.setloca le(locale.LC_AL L, 'English_United States.1252')
'English_United States.1252'
>conv = locale.localeco nv()
x = 1234567.8
locale.format( "%d", x, grouping=True)
'1,234,567'
>locale.format( "%s%.*f", (conv['currency_symbo l'], conv['int_frac_digit s'], 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...surel y 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.localeco nv.

--
Jerry

Oct 17 '06 #4
jr wrote:
>>import locale
locale.setloc ale(locale.LC_A LL, 'English_United States.1252')
'English_Unite d States.1252'
Just tried the first 2 commands on win XP, Python 2.5 under Idle.
An Error is raised: "unsupporte d 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.setloc ale(locale.LC_A LL, "")
'de_DE.UTF-8'

or try something less specific:
>>locale.setloc ale(locale.LC_A LL, "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
8815
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 txtTotal.Text = lblAmtdue.Caption + Val(txtTotal.Text) 'tries to format contents of txtTotal.Text as currency; doesn't work txtTotal.Text =...
3
2419
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 Currency symbol?
3
2119
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 with an exclamation point in the bottom left part of the window. When I told my IE to display a notification about every error...it told me there...
1
2137
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 cost field in the query are single and have decimal property set to three. Any ideas why I am getting such a long number after using the Sum()...
7
4527
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
3077
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 Binding("Text", Me.Ds1, "Items.BidAmt") txtBidAmt.DataBindings.Add(oBidAmt) Private Sub oBidAmt_Format(ByVal sender As Object, ByVal e As...
6
8481
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
2211
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 properties in the form to no avail. Any ideas? Thanks in advance
2
2124
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 number/currency format itself .Data exported to excel are all exported as text. Export to excel should maintain the formatting like numbers and money...
7
2051
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 14.899,01
0
7698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7673
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7970
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.