Math. Round (11.5) and so on? Math. Round (-11.5) and so on?
The math class provides three methods related to rounding: Ceil, floor, and round. The functions of these methods correspond to the meanings of their English names. For example, the English meaning of Ceil is the ceiling. This method indicates the rounded up, math. the result of Ceil (11.3) is 12, math. the result of Ceil (-11.3) is-11. The English meaning of floor is floor. This method indicates downgrading, math. the result of Ceil (11.6) is 11, math. the result of Ceil (-11.6) is-12. The most difficult to master is the round method, which indicates "Rounding" and the algorithm is math. floor (x + 0.5), which adds 0.5 to the original number and then rounds down. Therefore, math. the result of round (11.5) is 12, math. the result of round (-11.5) is-11.
System.out.println(Math.round(-11.5)+" "+Math.floor(-11.5+0.5)); //-11 System.out.println(Math.round(-11.6)+" "+Math.floor(-11.6+0.5));//-12
1 // 四舍五入 2 System.out.println(Math.round(11.4)); 3 System.out.println(Math.round(11.6)); 4 System.out.println(Math.round(-11.2)); 5 System.out.println(Math.round(-11.6)); 6 // 向上取整 7 System.out.println(Math.ceil(11.6)); 8 System.out.println(Math.ceil(-11.4)); 9 // 向下取整10 System.out.println(Math.floor(11.6));11 System.out.println(Math.floor(-11.4));
// 四舍五入 System.out.println(Math.round(11.4));//11 System.out.println(Math.round(11.5));//12 System.out.println(Math.round(-11.4));//-11 System.out.println(Math.round(-11.5)); //-11 System.out.println(Math.round(-11.6));//-12
Math. Round () Ceil floor