float formatting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian

    #1

    float formatting

    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

  • Diez B. Roggisch

    #2
    Re: float formatting

    > However, if I do that I get errors saying that all args were not[color=blue]
    > converted during string formatting. An escaped 'i' does not work
    > either.[/color]

    You need to format the string twice - first, to generate the float
    formatting string, and then to format the string.


    Like this:

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



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

    Diez

    Comment

    • Dave Hansen

      #3
      Re: float formatting

      On 25 Jan 2006 11:32:27 -0800 in comp.lang.pytho n, "Brian"
      <bnblazer@gmail .com> wrote:
      [color=blue]
      >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
      >[/color]

      Try something like this
      [color=blue][color=green][color=darkred]
      >>> num2 = 42.98765
      >>> for i in range(2,7):[/color][/color][/color]
      print "|" + "%10.*f"%(i,num 2) + "|"


      | 42.99|
      | 42.988|
      | 42.9877|
      | 42.98765|
      | 42.987650|[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      HTH,
      -=Dave

      --
      Change is inevitable, progress is not.

      Comment

      • Peter Otten

        #4
        Re: float formatting

        Brian wrote:
        [color=blue]
        > 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.[/color]

        In the http://en.wikipedia.org/wiki/Best_of...ossible_worlds you have to
        use a '*' instead of the 'i':
        [color=blue][color=green][color=darkred]
        >>> for i in range(2, 7):[/color][/color][/color]
        .... 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:
        [color=blue][color=green][color=darkred]
        >>> "|%*.*f|" % (10, 3, 1.23456)[/color][/color][/color]
        '| 1.235|'

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

        Peter

        Comment

        • Tim Chase

          #5
          Re: float formatting

          > for i in range(2,7):[color=blue]
          > print "|" + "%10.if" % num2 + "|" #where i would be the iteration
          > and the num of decimal places[/color]
          [color=blue][color=green][color=darkred]
          >>> for places in range(2,7):[/color][/color][/color]
          ... 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



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

          -tkc






          Comment

          • Brian

            #6
            Re: float formatting

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

            Thanks,
            Brian

            Comment

            • Peter Hansen

              #7
              Re: float formatting

              Brian wrote:[color=blue]
              > Thanks guys, that really helped. Am I to assume that the * references
              > the args in the parens?[/color]

              Better than assuming, you can read all about it:



              -Peter

              Comment

              Working...