Sep 23, 2024
5.3
(double) to 5
(int).
(int) 5.3
x = 5
(int) to 5.0
(double).
(double) x
x = 10.3
(double) to 10
(int).
(int) x
int total = 100;
int numPeople = 40;
double average = total / numPeople;
2.0
due to integer division.Goal: Obtain a decimal result, e.g., 2.5
.
Method: Cast one operand to a double.
double average = (double) total / numPeople;
2.5
.Alternative: Cast numPeople
instead of total
also yields correct result.