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'
Answer from S.Lott on Stack OverflowSee 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'
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
how to handle Currency in python ?
python - Converting Float to Dollars and Cents - Stack Overflow
Help With Formatting Int Values in List
How do I show currency with two decimal places
Videos
So i have been webscraping a lot in the last two weeks and i come across currencies, some have whole value while others are decimals.
Now how can i convert them to decimal with two digits after the decimal point ?
example:
$10
$11.56
$12.99
Output i Want:
10.00
11.56
12.99
i have tried round(float(10.00),2) after of course removing the $ sign with replace..
but i end up with 10.0, 11.5 and 12.9
Please Help :)
» pip install money
In Python 3.x and 2.7, you can simply do this:
>>> '${:,.2f}'.format(1234.5)
'$1,234.50'
The :, adds a comma as a thousands separator, and the .2f limits the string to two decimal places (or adds enough zeroes to get to 2 decimal places, as the case may be) at the end.
In python 3, you can use:
import locale
locale.setlocale( locale.LC_ALL, 'English_United States.1252' )
locale.currency( 1234.50, grouping = True )
Output
'$1,234.50'