Formatted Output - Floating Point Numbers

Doug Fort doug.fort at verizon.net
Sat Nov 16 16:39:09 EST 2002


locale is kind of quirky, but it will do what you want. It will also do
thousand separators (commas here in the US).

Python 2.2.2 (#1, Oct 20 2002, 07:17:53)
[GCC 3.2 20020903 (Red Hat Linux 8.0 3.2-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.setlocale(locale.LC_ALL, "")
>>> n = 57.0 / 7.0
>>> n
8.1428571428571423
>>> locale.format("%.2f", n, 1)
'8.14'
-- 
Doug Fort, Programmer
http://www.dougfort.net




More information about the Python-list mailing list