944,124 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 5014
  • Python RSS
Mar 25th, 2006
0

Weird problem with float or double....

Expand Post »
simple problem....but I have no clue why its not working.....

Python Syntax (Toggle Plain Text)
  1. if (w[0] > m[1]):
  2. temp = ((w[0]-m[1])/w[0])*100
  3. print temp


For some reason temp is always 0.........I want a double percentage difference between the two values....

Please guys..help me out here......after 500 lines of code ...I am stuck here......my assignment is due tonight.....

how the hell do I get a float value?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shafter111 is offline Offline
6 posts
since Feb 2006
Mar 25th, 2006
0

Re: Weird problem with float or double....

Hi shafter111,

If you are dividing an integer by some other number and you want the quotient to be a float, you need to cast the integer to a float. In other words:
Python Syntax (Toggle Plain Text)
  1. a, b = 8, 3
  2. temp = ((a-b)/a)*100 # Gives you zero
While:
Python Syntax (Toggle Plain Text)
  1. a, b = 8, 3
  2. temp = (float(a-b)/a)*100 # Gives you 62.5
If you need greater precision than that provided by float(), look up the decimal module in the tutorial:

Guido van Rossum's Python Tutorial

Hope that helps.
Reputation Points: 41
Solved Threads: 31
Junior Poster
G-Do is offline Offline
146 posts
since Jun 2005
Mar 25th, 2006
0

Re: Weird problem with float or double....

Thanks man......really appretiate the help
you rock!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shafter111 is offline Offline
6 posts
since Feb 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: problem in finding and replacing a word in python
Next Thread in Python Forum Timeline: Problem extracting values of a Dictionary





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC