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

View: New views
3 Messages — Rating Filter:   Alert me  

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

by Lorenzo Di Gregorio :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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
--
http://mail.python.org/mailman/listinfo/python-list

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

by Lutz Horn-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Lorenzo Di Gregorio schrieb:
> 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?

print '%05.2F' % 3.5

Lutz
--
http://mail.python.org/mailman/listinfo/python-list

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

by Chris Rebert-6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Nov 4, 2009 at 12:04 AM, Lorenzo Di Gregorio
<lorenzo.digregorio@...> wrote:

> 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?

>>> print ("%.2f" % 3.5).zfill(5)
03.50
>>> print ("%5.2f" % 3.5).replace(' ','0')
03.50

Cheers,
Chris
--
http://blog.rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list