# Thread: (Short) Integer Division into float variable

1. ## (Short) Integer Division into float variable

I'm tearing my hair out (again). This time I will simplify:

Code:
```unsigned short foo1 = 31 ;
unsigned short foo2 = 49 ;
float bar ;
bar = foo1/foo2 ; // I get zero. I want the result to be 0.633...```
I've tried putting (float) all over the division, same result, 0.

2. Recall that integer division is perform when both operands are integers. However, if one of the operands is an integer and the other is a float, double, or long double, the usual arithmetic conversions will convert the other operand into float, double, or long double respectively. Therefore:
Code:
`bar = (float)foo1 / foo2;`

3. Thank you, I thought I had tried that, but now it works. Weird!

4. Originally Posted by nigelmercier
I thought I had tried that, but now it works. Weird!
Obviously, you made a mistake, which is why instead of saying "I've tried putting (float) all over the division", you should have posted the code that you actually tried. For example, this is "(float) all over the division":
Code:
`bar = (float)(foo1 / foo2);`
But it is obviously wrong because being "all over the division" doesn't help when the division is still integer division.

5. Yup, you are right.

6. Just a follow up, I had to use the code below, must be my compiler...

Code:
`bar = (float)(foo1) / (float)(foo2);`

Popular pages Recent additions