954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Weird problem with float or double....

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

if (w[0] > m[1]):
      temp = ((w[0]-m[1])/w[0])*100
      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..... :sad:

how the hell do I get a float value? :sad:

shafter111
Newbie Poster
6 posts since Feb 2006
Reputation Points: 10
Solved Threads: 0
 

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:

a, b = 8, 3
temp = ((a-b)/a)*100     # Gives you zero

While:

a, b = 8, 3
temp = (float(a-b)/a)*100     # Gives you 62.5

If you need greater precision than that provided by float(), look up thedecimal module in the tutorial:

Guido van Rossum's Python Tutorial

Hope that helps.

G-Do
Junior Poster
147 posts since Jun 2005
Reputation Points: 41
Solved Threads: 31
 

Thanks man......really appretiate the help
you rock!

shafter111
Newbie Poster
6 posts since Feb 2006
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You