float formatting

Brian bnblazer at gmail.com
Wed Jan 25 20:32:27 CET 2006


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




More information about the Python-list mailing list