469,361 Members | 2,408 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

How to show percentage

Dear All:
I have a question of show percentage.
For example ,I want to show the percentage of 1/3 = 33.33%

I use the 1*100/3 = 33
it is 33 not 33.33 , how to show the 33.33 %
Thanks

Sep 22 '05 #1
7 17083
Traditionally, one part of the expression has to be a float for the
result to be a float (this is a holdover from C). So 100/3.0 will give
you the result you want. Alternatively, you can put "from __future__
import division" at the top of your script, and then 100/3 will return
a float.

http://www.python.org/doc/2.2.3/whatsnew/node7.html

Sep 22 '05 #2
You need to convert 1 or 3 to a float. How about:
def pct(num, den): return (float(num)/den) * 100 .... pct(1, 3)
33.333333333333329

Peace
Bill Mill
bill.mill at gmail.com

On 22 Sep 2005 10:51:43 -0700, Sen-Lung Chen
<sl****@slugger.ee.nthu.edu.tw> wrote: Dear All:
I have a question of show percentage.
For example ,I want to show the percentage of 1/3 = 33.33%

I use the 1*100/3 = 33
it is 33 not 33.33 , how to show the 33.33 %
Thanks

--
http://mail.python.org/mailman/listinfo/python-list

Sep 22 '05 #3
"Sen-Lung Chen" <sl****@larc.ee.nthu.edu.tw> writes:
Dear All:
I have a question of show percentage.
For example ,I want to show the percentage of 1/3 = 33.33%

I use the 1*100/3 = 33
it is 33 not 33.33 , how to show the 33.33 %


Python interprets '/' in an integer environment to return ints, not
floats. You can either turn one of your arguments into floats:
float(1 * 100) / 3 33.333333333333336

This is going to change. Not sure when. You can ask for the new behavior:
from __future__ import division
100 / 3

33.333333333333336

<mike

--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Sep 22 '05 #4
"Sen-Lung Chen" <sl****@larc.ee.nthu.edu.tw> wrote:
Dear All:
I have a question of show percentage.
For example ,I want to show the percentage of 1/3 = 33.33%

I use the 1*100/3 = 33
it is 33 not 33.33 , how to show the 33.33 %
Thanks


"%.2f%%" % (100./3.)

Not quite the most readable expression in python.. ;)

George
Sep 22 '05 #5
On Thursday 22 September 2005 12:51 pm, Sen-Lung Chen wrote:
Dear All:
I have a question of show percentage.
For example ,I want to show the percentage of 1/3 = 33.33%

I use the 1*100/3 = 33
it is 33 not 33.33 , how to show the 33.33 %
Thanks


In addition to needing a floating point value as other
posters have noted, you're going to need a format string,
e.g.:
print "%5.2f%%" % (100.0/3)

33.33%

Note the need to escape "%" as it is normally the format
specifier character.

Cheers,
Terry

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com

Sep 22 '05 #6
Sen-Lung Chen wrote:
Dear All:
I have a question of show percentage.
For example ,I want to show the percentage of 1/3 = 33.33%

I use the 1*100/3 = 33
it is 33 not 33.33 , how to show the 33.33 %
Thanks

You should by now know enough answers. The easiest way to ensure that
the result is floating point is to cast it as

pct = 100.0 * v) / N

This is guaranteed to work in all past and future Python versions
without setting any options. Then you can format is using the % operator.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.pycon.org

Sep 23 '05 #7
Steve Holden wrote:
Sen-Lung Chen wrote:
Dear All:
I have a question of show percentage.
For example ,I want to show the percentage of 1/3 = 33.33%

I use the 1*100/3 = 33
it is 33 not 33.33 , how to show the 33.33 %
Thanks


You should by now know enough answers. The easiest way to ensure that
the result is floating point is to cast it as

pct = 100.0 * v) / N

This is guaranteed to work in all past and future Python versions
without setting any options. Then you can format is using the % operator.

regards
Steve


.... apart from the obvious syntax error, that is. Sigh.

pct = (100.0 * v) / N

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.pycon.org

Sep 26 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Conax | last post: by
22 posts views Thread by Les Juby | last post: by
3 posts views Thread by Jeff S | last post: by
7 posts views Thread by Steve Kallal | last post: by
5 posts views Thread by James Conrad StJohn Foreman | last post: by
1 post views Thread by tru | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.