views:

4700

answers:

6

Hey,

I am looking to format a number like 188518982.18 to £188,518,982.18 using Python.

How can I do this?

Cheers

+26  A: 

See the locale module.

This does currency (and date) formatting.

>>>import locale
>>> locale.setlocale( locale.LC_ALL, '' )
'English_United States.1252'
>>> locale.currency( 188518982.18 )
'$188518982.18'
>>> locale.currency( 188518982.18, grouping=True )
'$188,518,982.18'
S.Lott
How would I format a non-native currency correctly, Say I'm showing a cost in GB pounds for a Japanese language report?
TokenMacGuy
@TokenMacGuy: That's a Trick Question. Japanese report means japanese comma and decimal place rules but GB Pound currency symbol -- not trivially supported by Locale. You have to create a customized locale definition.
S.Lott
if giver number is negative returns the value between "( )" why?
panchicore
@panchicore: I'm not sure I understand the question. Negative numbers are shown in ()'s for exactly one trivially obvious reason: that's the locale's of negative currency format. What's your real question?
S.Lott
+1  A: 

Oh, that's an interesting beast.

I've spent considerable time of getting that right, there are three main issues that differs from locale to locale: - currency symbol and direction - thousand separator - decimal point

I've written my own rather extensive implementation of this which is part of the kiwi python framework, check out the LGPL:ed source here:

http://svn.async.com.br/cgi-bin/viewvc.cgi/kiwi/trunk/kiwi/currency.py?view=markup

The code is slightly Linux/Glibc specific, but shouldn't be too difficult to adopt to windows or other unixes.

Once you have that installed you can do the following:

>>> from kiwi.datatypes import currency
>>> v = currency('10.5').format()

Which will then give you:

'$10.50'

or

'10,50 kr'

Depending on the currently selected locale.

The main point this post has over the other is that it will work with older versions of python. locale.currency was introduced in python 2.5.

Johan Dahlin
Does it have advantages over locale.currency() ?
Ali A
Only that it works in pre-python 2.5.
Johan Dahlin
A: 

I've come to look at the same thing and found python-money not really used it yet but maybe a mix of the two would be good

James Brooks
+4  A: 

My locale settings seemed incomplete, so I had too look beyond this SO answer and found:

http://docs.python.org/library/decimal.html#recipes

OS-independent

Just wanted to share here.

+2  A: 

New in 2.7

>>> '{:20,.2f}'.format(18446744073709551616.0)
'18,446,744,073,709,551,616.00'

http://docs.python.org/dev/whatsnew/2.7.html#pep-0378

nate c
A: 

If you are using OSX and have yet to set your locale module setting this first answer will not work you will receive the following error:

Traceback (most recent call last):File "<stdin>", line 1, in <module> File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/locale.py", line 221, in currency
raise ValueError("Currency formatting is not possible using "ValueError: Currency formatting is not possible using the 'C' locale.

To remedy this you will have to do use the following:

locale.setlocale(locale.LC_ALL, 'en_US')
simoes