Integer division: How do you produce a double?
In Java, integer division always produces an integer result, even if the dividend is a floating-point number. To produce a double result, you need to use floating-point division.
Here's an example of integer division:
int a = 5;
int b = 2;
int c = a / b; // c is 2
To produce a double result, you can either use double values for a
and b
, or you can cast one of the operands to double:
double a = 5.0;
int b = 2;
double c = a / b; // c is 2.5
int a = 5;
int b = 2;
double c = (double) a / b; // c is 2.5
I hope this helps! Let me know if you have any questions.