String formatting with floats... can't get it!

Skip Montanaro skip at pobox.com
Wed Mar 24 12:56:06 EST 2004


    >>> '%2.2f' % 2.33
    '2.33'

    Facundo> Why in this last line I don't get ' 2.33'?

Because the total field width you declared was only 2.  You need a field
width of at least 5 to get a space preceeding the one's digit:

    >>> '%5.2f' % 2.33
    ' 2.33'

The two digits in the format specifier don't add.  The first digit is the
overall width of the field.  The second digit is how much precision to
display to the right of the decimal point.

Skip




More information about the Python-list mailing list