Math类中提供了三个与取整有关的方法:
ceil、天花板,向上取整
floor、地板,向下取整
round,四舍五入
下面程序结果与上述规则相符:
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
}
}
网友评论