# math with integers

• 09-14-2005
FoodDude
math with integers
I have a variable that's an int.

I say variable = (50 /2000) * 100

My code say variable = 0.

What gives?

Code:

```int varx; varx = (50 /2000) * 100;```
oops, yes I know that int doesn't take decimals.
• 09-14-2005
ZuK
50/200 equals 0 if you use int's
if a result of 2 would be good enough you could do
Code:

`int varx = (50*100) /2000;`
Kurt
• 09-14-2005
FoodDude
Thanks, I might try that. I tried using a result dimensioned as a double but that also gave zero.
• 09-14-2005
ZuK
Quote:

Originally Posted by FoodDude
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 way
Code:

`double varx = (50.0 /2000.0) * 100.0;`
Kurt
• 09-14-2005
FoodDude
Thanks, I didn't realize the input type was tied so closely to the output type. I'll head down that path.
• 09-14-2005
Enahs
Quote:

Thanks, I might try that. I tried using a result dimensioned as a double but that also gave zero.
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.

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!
• 09-14-2005
FoodDude
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.