How to show percentage

S

Sen-Lung Chen

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
 
D

dylan.moreland

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
 
B

Bill Mill

You need to convert 1 or 3 to a float. How about:
33.333333333333329

Peace
Bill Mill
bill.mill at gmail.com
 
M

Mike Meyer

Sen-Lung Chen said:
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:
33.333333333333336

This is going to change. Not sure when. You can ask for the new behavior:
33.333333333333336

<mike
 
G

George Sakkis

Sen-Lung Chen said:
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
 
T

Terry Hancock

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.:33.33%

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

Cheers,
Terry
 
S

Steve Holden

Sen-Lung Chen said:
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
 
S

Steve Holden

Steve said:
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
 

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

Forum statistics

Threads
474,264
Messages
2,571,323
Members
48,006
Latest member
TerranceCo

Latest Threads

Top