Math類中提供了三個與取整有關的方法:
ceil、天花板哀卫,向上取整
floor府蔗、地板,向下取整
round峡谊,四舍五入
下面程序結果與上述規(guī)則相符:
public class Test{
public static void main(String[] args) {
System.out.println(Math.round(11.5)); //12
System.out.println(Math.round(-11.5)); //-11
System.out.println(Math.ceil(11.5)); //12
System.out.println(Math.ceil(-11.5)); //-11
System.out.println(Math.floor(11.5)); //-11
System.out.println(Math.floor(-11.5)); //-12
}
}