I want to multiply these values.
int result,quantity = 10;
double quantity_col = 3.500;
result = quantity*quantity_col;
but the eclipse ide underlines it and gives me an error.
java.lang.Error cannot convert from double to int
I want to multiply these values.
int result,quantity = 10;
double quantity_col = 3.500;
result = quantity*quantity_col;
but the eclipse ide underlines it and gives me an error.
java.lang.Error cannot convert from double to int
The variable
result
is of typeint
, so, since there is adouble
in your multiplication, Eclipse does well to indicate the error, because in a multiplication between aint
and adouble
, the result obtained is adouble
and, as such, it must be stored in a variable of that kind. So your code should look like this:PS: always declare one attribute per line, for a better reading of the program.
To convert a value of type double to int you have to use what is known as casting or classes that Java provides to perform conversions. In your case, the code should look like this:
Another option is to cast:
And these are the two ways you can convert a double to an int.