java中的Math.ceil、Math.floor和Math.
2020-01-17 本文已影响0人
氨基钠
package com.company;
public class Main {
public static void main(String[] args) {
//向上取整
System.out.println(Math.ceil(11.3));//12.0
System.out.println(Math.ceil(-11.3));//-11.0
//向下取整
System.out.println(Math.floor(11.3));//11.0
System.out.println(Math.floor(-11.3));//-12.0
//四舍五入 算法为Math.floor(x+0.5) 即原来的数字加上0.5再向下取整
System.out.println(Math.round(11.4));//11
System.out.println(Math.round(11.5));//12
System.out.println(Math.round(11.6));//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
}
}