I can't get float value from int/int.
float rs;
int a=10;
int b=3;
rs=(float)a/b;
Result : 3.0 . I need 3.333 Thank!
I can't get float value from int/int.
float rs;
int a=10;
int b=3;
rs=(float)a/b;
Result : 3.0 . I need 3.333 Thank!
Testing your code, it does indeed give 3.333
, because the typecast takes precedence... did you execute some other code?
Another possible option is to typecast b
.
rs = a / (float)b;
You could also typecast a
, but you'd need an extra set of parenthesis.
Here's an ideone
demo.
EDIT: The cast has priority:
rs = (float) a / b;
Or,
float new_a = a;
rs = new_a / b;