472,119 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 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 5946
>>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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Bill Stanard | last post: by
3 posts views Thread by John H. | last post: by
3 posts views Thread by Abby Lee | last post: by
1 post views Thread by Newbie | last post: by
6 posts views Thread by John A Grandy | last post: by
7 posts views Thread by Nariban Barkan | last post: by
reply views Thread by leo001 | last post: by

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.