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 OverflowMonepy
A python package that implements currency classes to work with monetary values.
Target audience
I created it mostly for some data analysis tasks I usually do, and also as way to learn about project structure, documentation, github actions and how to publish packages.
I wouldn't know if it's production ready.
Comparison
After starting it I found about py-moneyed. They are quite similar, but I wanted something that looks "cleaner" when using it.
Any feedback will be appreciated.
Videos
» pip install currencies
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'
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
» pip install CurrencyConverter
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 :)
How about Babel?
from babel import numbers
print numbers.get_currency_symbol('USD', locale='en') # => $1,500.00
print numbers.get_currency_symbol('GBP', locale='fr_FR') # => 1 500,00 £UK
Using the locale module:
import locale
locales=('en_AU.utf8', 'en_BW.utf8', 'en_CA.utf8',
'en_DK.utf8', 'en_GB.utf8', 'en_HK.utf8', 'en_IE.utf8', 'en_IN', 'en_NG',
'en_PH.utf8', 'en_US.utf8', 'en_ZA.utf8',
'en_ZW.utf8', 'ja_JP.utf8')
for l in locales:
locale.setlocale(locale.LC_ALL, l)
conv=locale.localeconv()
print('{ics} ==> {s}'.format(ics=conv['int_curr_symbol'],
s=conv['currency_symbol']))
yields:
AUD ==> $
BWP ==> Pu
CAD ==> $
DKK ==> kr
GBP ==> £
HKD ==> HK$
EUR ==> €
INR ==> ₨
NGN ==> ₦
PHP ==> Php
USD ==> $
ZAR ==> R
ZWD ==> Z$
JPY ==> ¥
Note you need the locale information installed on your machine. On Ubuntu, this means having the right language-pack-* packages installed.
On *nix systems, you can find the list of known locales (e.g. en_GB.utf8) with
locale -a
I don't know of a way to obtain this list from within Python (without using subprocess).