What is happening is that 120 and 77 are integers. When the operator/(int,int)
function is performed, the return type is int
. You either need to have a floating point type value (120.0 & 77.0) or you can typecast one of the values. The C-style typecasting is available and simple, by placing the type to be casted as in parenthesis.
float num = (float)120/77;
However, in C++, static_cast<T>(T)
is preferred. Also, I would recommend using double
instead of float
.
double num = static_cast<double>(120) / 77;
Typically, casting is done for variables and not hard-coded values.