How to print zero-padded floating point numbers in python 2.6.1

  • Thread starter Lorenzo Di Gregorio
  • Start date
L

Lorenzo Di Gregorio

Hello,

I thought that I could zero-pad a floating point number in 'print' by
inserting a zero after '%', but this does not work.

I get:

print '%2.2F' % 3.5
3.50
print '%02.2F' % 3.5
3.50

How can I get print (in a simple way) to print 03.50?

Best Regards,
Lorenzo
 
C

Chris Rebert

Hello,

I thought that I could zero-pad a floating point number in 'print' by
inserting a zero after '%', but this does not work.

I get:

print '%2.2F' % 3.5
3.50
print '%02.2F' % 3.5
3.50

How can I get print (in a simple way) to print 03.50?
03.50

Cheers,
Chris
 
D

Dennis Lee Bieber

Hello,

I thought that I could zero-pad a floating point number in 'print' by
inserting a zero after '%', but this does not work.

I get:

print '%2.2F' % 3.5
3.50
print '%02.2F' % 3.5
3.50

The size codes are not <pre>.<post>... They are
<min-width>.<post>...

So above you've specified a zero-filled field of width 2, of which 2
of them come after the decimal point (leaving the decimal point and
anything in front of it as "oversized" data.

To get two digits in front, and two behind, you need to specify 5.2;
and from that, using a leading 0 should do zero-fill when the data is
less than that width.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top