Java distinguish Several rounding method of the Math

1. Math.ceil ()    rounding up

System.out.println (Math.ceil (3.4 ));
 // output. 4 
System.out.println (Math.ceil (3.7 ));
 // output. 4 
System.out.println (Math.ceil (-3.4 )) ;
 // output -3 
System.out.println (Math.ceil (-3.7 ));
 // output -3

2. Math.floor ()    rounds down

System.out.println (Math.floor (3.4 ));
 // output. 3 
System.out.println (Math.floor (3.7 ));
 // output. 3 
System.out.println (Math.floor (-3.4 )) ;
 // output -4 
System.out.println (Math.floor (-3.7 ));
 // output -4

3. Math.round ()   rounding [Special Note: negative numbers are rounded up to the big number)

Memory rule: the re-rounding down after the original number plus 0.5

System.out.println (Math.round (3.4 ));
 // output. 3 
System.out.println (Math.round (3.7 ));
 // output. 4 
System.out.println (Math.round (-3.4 )) ;
 // output -3 
System.out.println (Math.floor (-3.7 ));
 // output -4 
System.out.println (Math.floor (-3.5 ));
 // output -3

4. Supplement: int ()   to zero, regardless of the back less than 5 or greater than 5, should be removed

System.out.println (the Math. Int (3.4 ));
 // output. 3 
System.out.println (the Math. Int (3.7 ));
 // output. 3 
System.out.println (the Math. Int (-3.4 )) ;
 // output -3 
System.out.println (the Math. int (-3.7 ));
 // output -3

 

Guess you like

Origin www.cnblogs.com/iceywu/p/12014026.html