Casts and Rounding
This statement prints the average using integer division, e.g. 7/2 is 3:
g : System.out.println( sum/count );
This statement prints the average using “double precision floating point division”, e.g., (double)7/2 is 3.5:
An expression of the form
is a cast and converts the value of expression to a value of the given type, if possible.
This statement prints the average as a “rounded double precision floating point number”, e.g.,
Math.round((double)7/2) is 4:
g’’ : System.out.println(
Math.round((double)sum / count)
);