473,322 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 2449
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: David Sharp | last post by:
I'm trying to find a way to format a FLOAT variable into a varchar in SQL Server 2000 but using CAST/CONVERT I can only get scientific notation i.e. 1e+006 instead of 1000000 which isn't really...
7
by: nephish | last post by:
Hullo all ! i have a real easy one here that isn't in my book. i have a int number that i want to divide by 100 and display to two decimal places. like this float(int(Var)/100) but i need it...
2
by: hpy_awad | last post by:
formatting float variables to fprintf has error to my writing to output file called rental and I do not the reason that a rabish is written to the file instead of the actual input screen values ? ...
2
by: Abra | last post by:
Hello, I am using the XmlSerializer clas to serialize/deserialize XML files. I have a XML file containing elements which have attributes of type float. If the value of the float attribute in my...
22
by: ashkaan57 | last post by:
Hi, I am trying to put text on left and right side of the page and used: <div> <span>blah blah</span> <span style="float:right">blah blah</span> </div> The 2nd text does go to the right but the...
14
by: Jim Langston | last post by:
The output of the following program is: 1.#INF 1 But: 1.#INF 1.#INF was expected and desired. How can I read a value of infinity from a stream?
3
by: =?Utf-8?B?U2FzaQ==?= | last post by:
create a J# windows applicaion.add a button , a label and two textboxes to form and paste this code into the buttons event handler: private void button1_Click(Object sender, System.EventArgs e)...
5
by: Selvam | last post by:
Hi All, I am getting exponent value when doing float arithmetic in C++. Instead of that I need accurate value. float amount = 0.0f; float x = 0.99999976f; amount= 1.0f - x; I am getting...
3
by: Ben C | last post by:
On 2008-04-07, Gus Richter <gusrichter@netscape.netwrote: No the containing block for a float is always above it in the document tree. But floats "invade" subsequent blocks in the same block...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.