# Thread: using float but getting an integer

1. ## using float but getting an integer

Im trying to get final to be a decimal, however it couts final as an integer, rounded to the one's place value.

Code:
```float intimcompar(int intimidation, int intimidation2)
{
int rndno1, rndno2, mod1, mod2;
float final;
rndno1=(rand() % 1000+1)+(rand() % 1000+1);
rndno2=(rand() % 1000+1)+(rand() % 1000+1);
mod1=intimidation-rndno1;
mod2=intimidation2-rndno2;
final=(mod1-mod2)/166;
cout << intimidation << " " << intimidation2 << " " << rndno1 << " " << rndno2 << " " << mod1 << " " << mod2 << " " << final << " \n";;
return final;
}```

2. try this:
final = ((float)mod1-mod2)/166;

3. final=(mod1-mod2)/166;

To the right of the equal sign is an integer expression, which is evaluated and then truncated. That value is then assigned to final, which effectively just adds:

.0

to the integer value.

4. the above code suggestion works, but Im not sure why it works. could you explain how and when Im supposed to use (float) in an equation?

5. When you divide an integer by another integer, you get an integer result. Therefore, if you want a float result, you must type cast at least one of the integers to a float, so then the divide will be a float divide. If you only type case one of the integers to a float, the compiler will actually convert the other integer to a float, for you, since it can only divide floats by other floats.

Note that the result of the minus operation is an integer, and if you type cast one of the integers in the minus operation, its result will be a float, which is sufficent to have one of the operands of the divide be a float, and thus a float divide will be performed.

6. you could also make one of the variables at the top a float/double. then you wouldn't have to worry about typecasting. just another option.