数学工具类Math
package com.Demo06;
/*
java.util.Math类时数相关的工具类,里面提供了大量的静态方法,完成数学相关的操作
public static double abs(double num):获取绝对值,有多种重载
public static double ceil(double num):向上取整
public static double floor(double num):向下取整
public static long round(double num):四舍五入
Math.PI 代表近似的圆周率常量 (double)
*/
public class Demo01Math {
public static void main(String[] args) {
//绝对值
System.out.println(Math.abs(0.1)); //0.1
System.out.println(Math.abs(-0.1)); //0.1
System.out.println(Math.abs(1)); //1
//向上取整
System.out.println(Math.ceil(2.1)); //3.0
System.out.println(Math.ceil(2.9)); //3.0
System.out.println(Math.ceil(2.0)); //2.0
//向下取整
System.out.println(Math.floor(3.1)); //3.0
System.out.println(Math.floor(3.9)); //3.0
System.out.println(Math.floor(3.0)); //3.0
//四舍五入
System.out.println(Math.round(4.4)); //4
System.out.println(Math.round(4.9)); //5
System.out.println(Math.round(4.5)); //5
}
}