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

Discussion in 'Python' started by Lorenzo Di Gregorio, Nov 4, 2009.

  1. 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
    Lorenzo Di Gregorio, Nov 4, 2009
    #1
    1. Advertising

  2. Lorenzo Di Gregorio

    Lutz Horn Guest

    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
    Lutz Horn, Nov 4, 2009
    #2
    1. Advertising

  3. Lorenzo Di Gregorio

    Chris Rebert Guest

    On Wed, Nov 4, 2009 at 12:04 AM, Lorenzo Di Gregorio
    <> 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
    Chris Rebert, Nov 4, 2009
    #3
  4. On Wed, 4 Nov 2009 00:04:08 -0800 (PST), Lorenzo Di Gregorio
    <> declaimed the following in
    gmane.comp.python.general:

    > 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.
    --
    Wulfraed Dennis Lee Bieber KD6MOG
    HTTP://wlfraed.home.netcom.com/
    Dennis Lee Bieber, Nov 4, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Motaz Saad
    Replies:
    7
    Views:
    6,482
  2. , India

    floating point zero value from c-faq.com

    , India, Mar 7, 2007, in forum: C Programming
    Replies:
    5
    Views:
    450
    Mark McIntyre
    Mar 7, 2007
  3. Saraswati lakki
    Replies:
    0
    Views:
    1,321
    Saraswati lakki
    Jan 6, 2012
  4. teeshift
    Replies:
    2
    Views:
    252
    Chris Pearl
    Dec 1, 2006
  5. thiel
    Replies:
    4
    Views:
    119
    thiel
    Nov 4, 2008
Loading...

Share This Page