I have a variable that's an int.
I say variable = (50 /2000) * 100
The logical answer is 2.5
My code say variable = 0.
What gives?
oops, yes I know that int doesn't take decimals.Code:int varx; varx = (50 /2000) * 100;
I have a variable that's an int.
I say variable = (50 /2000) * 100
The logical answer is 2.5
My code say variable = 0.
What gives?
oops, yes I know that int doesn't take decimals.Code:int varx; varx = (50 /2000) * 100;
Last edited by FoodDude; 09-14-2005 at 11:10 AM.
50/200 equals 0 if you use int's
if a result of 2 would be good enough you could do
KurtCode:int varx = (50*100) /2000;
Thanks, I might try that. I tried using a result dimensioned as a double but that also gave zero.
That doesn't help because 50 is still an int. You have to do it this wayOriginally Posted by FoodDude
KurtCode:double varx = (50.0 /2000.0) * 100.0;
Thanks, I didn't realize the input type was tied so closely to the output type. I'll head down that path.
The variable you store in needs to be of type double. But in your addition with hard coded numbers if you use two ints, your result will be ints. 50/2000 is 0.025, but it is of type int so it only keeps what is in front of the decimal, then multiplies that by 100. Which is 0.Thanks, I might try that. I tried using a result dimensioned as a double but that also gave zero.
Make 50, 50.0 or 2000, 2000.0, or both and it will work just fine.
*edit*
Bah, while I was typing this somebody else answered!
Thanks for the help. I am using variables in my code instead of hard-coded numbers but was using hard-coded in some debugging to try and understand it. I've changes 100 to 100.0 and changed the other values to doubles. All is good. Thanks to all.