Brian wrote: > 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.
In the http://en.wikipedia.org/wiki/Best_of_all_possible_worlds you have to use a '*' instead of the 'i': >>> for i in range(2, 7): ... 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: >>> "|%*.*f|" % (10, 3, 1.23456) '| 1.235|' See http://docs.python.org/lib/typesseq-strings.html for the full story. Peter -- http://mail.python.org/mailman/listinfo/python-list