-
conversions
Hi again
For love of god, how can I convert a integer to a float??
I have this function that returns always zero:
int iDrawIHeight = 480;
iScreenHeight = 768;
float iPorcentage = ((float)(iDrawedHeight/iScreenHeight));
The result that must returned is 0.62, but it returns zero. Why?
-
Re: conversions
Because 480/768 is zero, when you're dealing with integers. Convert them to float before doing the division. Like this:
float iPorcentage = ((float)iDrawedHeight)/(float)iScreenHeight;
-
Re: conversions
What is happening is that you are taking two integer values 480/768 which equals 0 and then casting that 0 value to a float. You need to do what torfil mentioned.
The second cast "(float)iScreenHeight" is optional since iScreenHeight will automatically be converted to a float.
-
Re: conversions
thanks guys, the code now works
-
Re: conversions
Use static_cast<>. See the details here - msdn - static_cast<> operator. Regards.