float formatting

B

Brian

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
 
D

Diez B. Roggisch

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.

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
 
D

Dave Hansen

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

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


| 42.99|
| 42.988|
| 42.9877|
| 42.98765|
| 42.987650|
HTH,
-=Dave
 
P

Peter Otten

Brian said:
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':
.... 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
 
T

Tim Chase

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

>>> 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
 
B

Brian

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

Thanks,
Brian
 

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

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top