468,110 Members | 1,930 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,110 developers. It's quick & easy.

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

Jan 25 '06 #1
6 2272
> 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
Jan 25 '06 #2
On 25 Jan 2006 11:32:27 -0800 in comp.lang.python, "Brian"
<bn******@gmail.com> wrote:
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
num2 = 42.98765
for i in range(2,7): print "|" + "%10.*f"%(i,num2) + "|"
| 42.99|
| 42.988|
| 42.9877|
| 42.98765|
| 42.987650|


HTH,
-=Dave

--
Change is inevitable, progress is not.
Jan 25 '06 #3
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...ossible_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

Jan 25 '06 #4
> 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


Jan 25 '06 #5
Thanks guys, that really helped. Am I to assume that the * references
the args in the parens?

Thanks,
Brian

Jan 25 '06 #6
Brian wrote:
Thanks guys, that really helped. Am I to assume that the * references
the args in the parens?


Better than assuming, you can read all about it:

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

-Peter

Jan 25 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by nephish | last post: by
2 posts views Thread by hpy_awad | last post: by
22 posts views Thread by ashkaan57 | last post: by
14 posts views Thread by Jim Langston | last post: by
3 posts views Thread by =?Utf-8?B?U2FzaQ==?= | last post: by
5 posts views Thread by Selvam | last post: by
3 posts views Thread by Ben C | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.