Leading zeroes problem

Peter Hansen peter at engcorp.com
Thu May 16 21:03:40 EDT 2002


jb wrote:
> 
> How do I get '00.0' from 0.0. I can say '%03d' % 0 but with floating point
> numbers this does not work.

The others gave you the right answer, but only Tino pointed you to
where you could find the explanations.  In any case, the reason what
you tried didn't work is that the "3" in the example above represents
the _field_ width, not the number of digits.  For an integer the
field width is the same as the size of the number, but for a float
there's also a decimal point and the fractional digits.  So 00.0 is
really 4 characters wide, thus you need the %04.1f .

-Peter



More information about the Python-list mailing list