150/151数学工具类math
数学工具类Math
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)。
1 public static void main(String[] args) {
2 //获取绝对值
3 System.out.println(Math.abs(3.14));
4 System.out.println(Math.abs(0));
5 System.out.println(Math. abs(-2.5));
6 System.out.println( "================");
7 //向上取整
8 System.out.println(Math.ceil(3.9));
9 System.out.println(Math.ceil(3.1));
10 System.out.println(Math.ceil(3.0));
11 System.out.println( "================");
12 //向下取整,抹零
13 System.out.println(Math.floor( 30.1));
14 System.out.println(Math.floor( 30.9));
15 System.out.println(Math.floor( 31.0));
16 System.out.println( "================");
17 System.out.println(Math.round(20.4));
18 System.out.println(Math.round(10.5));
19 }