
The round Method
The round method converts a double to its nearest integer using these rules:
■
any factional part .0 to .4 is rounded down
■
any fractional part .5 and above is rounded up
Lines 9–13 in Example 3.15 use the round method with various numbers.
Figure 3.22 shows the output.
1 /* A demonstration of the Math round method
2 Anderson, Franceschi
3 */
4
5 public class MathRounding
6 {
7 public static void main( String [ ] args )
8 {
9 System.out.println( “23.4 rounded is “ + Math.round( 23.4 ));
10 System.out.println( “23.49 rounded is “ + Math.round( 23.49 ));
11 System.out.println( “23.5 rounded is “ + Math.round( 23.5 ));
12 System.out.println( ...