[Tutor] Re: printing monetary values

Derrick 'dman' Hudson dman@dman.ddts.net
Mon, 26 Aug 2002 19:42:13 -0400


--0OAP2g/MAC+5xKAE
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Mon, Aug 26, 2002 at 02:51:01PM -0700, Sean 'Shaleh' Perry wrote:
| 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.
|
| import locale

| locale.setlocale(locale.LC_ALL, "") # sets locale to the currently defined
|                                              # value in the user's enviro=
nment
  **********************************

| locale_info =3D locale.localeconv()
| currency =3D locale_info['currency_symbol']
|=20
| # now that we have the currency symbol we call locale.format()
| # to transform the number into a pretty string
|=20
| total =3D 3275.68
| print "total: %s%s\n" % (currency, locale.format("%0.2f", total, 1))

Bingo!  The line emphasized by the row of stars is the key to making
this actually work.  Thanks!

-D

--=20
He who spares the rod hates his son,
but he who loves him is careful to discipline him.
        Proverbs 13:24
=20
http://dman.ddts.net/~dman/

--0OAP2g/MAC+5xKAE
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iEYEARECAAYFAj1qvNUACgkQO8l8XBKTpRQHcgCglT5Yj7C5KeOnr07XKZ10Xhww
DCwAoL63QO1QNW86uFg1MnOrYMG7ZIdC
=0Pyw
-----END PGP SIGNATURE-----

--0OAP2g/MAC+5xKAE--