float formatting

Discussion in 'Python' started by Brian, Jan 25, 2006.

  1. Brian

    Brian Guest

    Hello all,

    I am a bit stuck with a float formatting issue. What I want to do is
    print a float to the screen with each line showing one more decimal
    place. Here is a code snip that may explain it better:

    #!/usr/bin/env python

    num1 = 32
    num2 = 42.98765

    for i in range(2,7):
    print "|" + "%10.3f" % num2 + "|"

    In the for loop, is the line with my difficulty. In a perfect world it
    would read something like this:

    for i in range(2,7):
    print "|" + "%10.if" % num2 + "|" #where i would be the iteration
    and the num of decimal places

    However, if I do that I get errors saying that all args were not
    converted during string formatting. An escaped 'i' does not work
    either.

    Any thoughts would be appreciated.

    Thanks,
    Brian
     
    Brian, Jan 25, 2006
    #1
    1. Advertisements

  2. However, if I do that I get errors saying that all args were not
    You need to format the string twice - first, to generate the float
    formatting string, and then to format the string.


    Like this:

    num = 7.12345678901234567
    for i in xrange(3,7):
    print ("%%.%if" % i) % num



    Note the %% that produces a single % for the second string interpolation.

    Diez
     
    Diez B. Roggisch, Jan 25, 2006
    #2
    1. Advertisements

  3. Brian

    Dave Hansen Guest

    Try something like this
    print "|" + "%10.*f"%(i,num2) + "|"


    | 42.99|
    | 42.988|
    | 42.9877|
    | 42.98765|
    | 42.987650|
    HTH,
    -=Dave
     
    Dave Hansen, Jan 25, 2006
    #3
  4. Brian

    Peter Otten Guest

    In the http://en.wikipedia.org/wiki/Best_of_all_possible_worlds you have to
    use a '*' instead of the 'i':
    .... print "|%10.*f|" % (i, 42.98765)
    ....
    | 42.99|
    | 42.988|
    | 42.9877|
    | 42.98765|
    | 42.987650|

    You can replace the width constant with a star, too:
    '| 1.235|'

    See http://docs.python.org/lib/typesseq-strings.html for the full story.

    Peter
     
    Peter Otten, Jan 25, 2006
    #4
  5. Brian

    Tim Chase Guest

    for i in range(2,7):
    >>> for places in range(2,7):
    ... print "|%10.*f|" % (places, num2)
    ...
    | 42.99|
    | 42.988|
    | 42.9877|
    | 42.98765|
    | 42.987654|

    seems to do the trick.

    It exploits the "*" operator for specifying the precision as
    detailed in item #4 at

    http://docs.python.org/lib/typesseq-strings.html

    which is the same as in C/C++ formatting strings.

    -tkc
     
    Tim Chase, Jan 25, 2006
    #5
  6. Brian

    Brian Guest

    Thanks guys, that really helped. Am I to assume that the * references
    the args in the parens?

    Thanks,
    Brian
     
    Brian, Jan 25, 2006
    #6
  7. Brian

    Peter Hansen Guest

    Peter Hansen, Jan 25, 2006
    #7
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.