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
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 :)
python - Converting Float to Dollars and Cents - Stack Overflow
how to handle Currency in python ?
Adding $ or % in python without space in between the symbol and number.
You should look into pyformat when you only want to play around with the string representation.
>>> cash = 13.14
>>> '${}'.format(cash)
'$13.14'
>>> cash = 3.4
>>> '${:.2f}'.format(cash)
'$3.40'
>>> values = [1.2, 65.32, 15.2345]
>>> template = 'Stuff costs ${:.2f}.'
>>> for value in values:
print(template.format(value))
Stuff costs $1.20.
Stuff costs $65.32.
Stuff costs $15.23.You could also play around with object oriantation and manipulate the specific string manipulation:
>>> class Dollar:
def __init__(self, value):
self.value = value
def __add__(self, other):
return Dollar(self.value + other.value)
def __sub__(self, other):
return Dollar(self.value - other.value)
def __repr__(self):
return 'Dollar({})'.format(self.value)
def __str__(self):
return '${:.2f}'.format(self.value)
>>> d1 = Dollar(1.2)
>>> d2 = Dollar(4.63)
>>> d1
Dollar(1.2)
>>> str(d1)
'$1.20'
>>> d2
Dollar(4.63)
>>> str(d2)
'$4.63'
>>> d1+d2
Dollar(5.83)
>>> d1-d2
Dollar(-3.4299999999999997)
>>> print(d1+d2)
$5.83
>>> print(d1-d2)
$-3.43
>>> price = Dollar(0.99)
>>> 'The price is {}'.format(price)
'The price is $0.99'
See, with this double underscore methods you can mess around with the power of python. __add__ let's you override the plus operator, __str__ controls the string conversion and __repr__ the internal interpreter representation.
Of course you should convert your values to Decimal compute with that to avoid aweful rounding errors:
>>> from decimal import Decimal
>>> str(Decimal('0.1'))
'0.1'
>>> class Dollar:
def __init__(self, value):
self.value = Decimal(str(value))
def __add__(self, other):
return Dollar(self.value + other.value)
def __sub__(self, other):
return Dollar(self.value - other.value)
def __repr__(self):
return 'Dollar({})'.format(self.value)
def __str__(self):
return '${:.2f}'.format(self.value)
>>> d1 = Dollar(1.2)
>>> d2 = Dollar(4.63)
>>> d1
Dollar(1.2)
>>> str(d1)
'$1.20'
>>> d2
Dollar(4.63)
>>> str(d2)
'$4.63'
>>> d1+d2
Dollar(5.83)
>>> d1-d2
Dollar(-3.43)
>>> print(d1+d2)
$5.83
>>> print(d1-d2)
$-3.43
>>> price = Dollar(0.99)
>>> 'The price is {}'.format(price)
'The price is $0.99'Do you see the difference? ;)
Also there is alot more, but for the beginning is that enough.
More on reddit.comHow to format Currency without currency sign.
Videos
» pip install format-currency
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'