question about string formatting
Jerry Hill
malaclypse2 at gmail.com
Wed Apr 9 13:31:30 EDT 2008
On Wed, Apr 9, 2008 at 1:04 PM, Kelie <kf9150 at gmail.com> wrote:
> Is there something in Python built-in function or library that will convert
> a number 1205466.654 to $1,205,466.65? To add the "$" sign and set the
> decimal place is not a problem, but I don't know how to add the thousands
> delimiter.
The locale module provides this functionality.
>>> import locale
>>> locale.setlocale(locale.LC_ALL, '')
'English_United States.1252'
>>> print locale.currency(1205466.654)
$1205466.65
>>> locale.currency(1205466.654, grouping=true)
$1,205,466.65
That's using the default locale. For me, that's US English. Other
languages and countries have different ways of showing currency:
>>> locale.setlocale(locale.LC_ALL, "fr")
'French_France.1252'
>>> print locale.currency(1205466.654, grouping=True)
1 205 466,65 €
--
Jerry
More information about the Python-list
mailing list