I am newbie in Java and just trying to convert a variable according to math operation. Let’s have following scheme:
int var_result;
if(addition) {
var_result = number1+number2; // number1, number2 => integers
}
else if(division){
var_result = (float) number1 / number2;
// and here is the problem - I don't know, how to convert "var_result" from integer to float
}
...just print var result ...
Is there any quick way, how to convert the var_result variable in this case? The easiest way in this example would be don’t convert it and just use float var_result_float, but I don’t want to solve it this way…
Thanks
int var_result;is already declared. You can’t expect to store a float in it, without losing precision.When you perform type-casting, the variable is temporarily treated as another type for the purpose of evaluation of an expression but that does not change what it actually is.
An int remains an int. You should go with
float var_result_floatto store a float.