473,387 Members | 1,669 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,387 software developers and data experts.

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

Similar topics

7
by: Conax | last post by:
Hello, This is actually a general programming question but I don't know which newsgroup best to put it so please pardon me. I've always been looking for the best way to get an accurate...
22
by: Les Juby | last post by:
I am trying to adjust the window/table size of a website (www.worklaw.co.za) which has made use of DIV tags with its settings embedded in an CSS file. The client wants its width and height to...
3
by: Jeff S | last post by:
I'm enabling users to upload files to the server. I'd like to show them some indication of percent complete. How can this be done? Thanks!
7
by: Steve Kallal | last post by:
I have seen this subject tossed around in this forum before. But in my case I need a simple solution. I do NOT need to show progress in terms on percentage complete. But rather I need to show a...
0
by: fake ID | last post by:
Since you can't search for these symbols used in asp.net "<%#" or '<%=' I thought i'd post this to make things a little easier to find. Potential search word combinations: -lessthan Percentage...
5
by: James Conrad StJohn Foreman | last post by:
Have found http://www-128.ibm.com/developerworks/db2/library/techarticle/lyle/0110lyle.html which is quite helpful, but doesn't quite tell me what I want. I have a table, advertising_spend with...
4
by: J | last post by:
I am editing a pre-existing view. This view is already bringing data from 40+ tables so I am to modify it without screwing with anything else that is already in there. I need to (left) join it...
4
by: Micheal | last post by:
Greetings Access Group, Being relatively new to Access, I try to work through problems on my own and have been very successful, although I have a conundrum that I have been working on for two days...
1
by: tru | last post by:
i am doing software in "vb.net" i want to show map of maharashtra on frontscreen of my application. i can give that map as image to picturebox. but i want to show rainfall percentage at particular...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.