[Tutor] printing monetary values

Sean 'Shaleh' Perry shalehperry@attbi.com
Mon, 26 Aug 2002 14:51:01 -0700


On Monday 26 August 2002 02:50 pm, Derrick 'dman' Hudson wrote:
> I just know I've seen mention before of a library function that
> properly formats money as a string, but I can't seem to find it
> anywhere.  I want to receive a NUMERIC(15,2) data from a SQL database
> (eg, 3275.68) and have it displayed as $3,275.68.  (dollar sign,
> commas, and decimal)  I've searched the Vaults of Parnassus, the
> 'locale' module, and comp.lang.python, but didn't find anything except
> a few partial examples of how to implement it myself.
>
> TIA,
> -D

import locale
locale.setlocale(locale.LC_ALL, "") # sets locale to the currently define=
d
                                             # value in the user's enviro=
nment
locale_info =3D locale.localeconv()
currency =3D locale_info['currency_symbol']

# now that we have the currency symbol we call locale.format()
# to transform the number into a pretty string

total =3D 3275.68
print total: "%s%s\n" % (currency, locale.format("%0.2f", total, 1))