第二十章 Math 数学工具类
20.1 概述
- java.util.Math类是数学相关的工具类,里面提供了大量的静态方法,完成与数学运算的相关操作。
20.2 绝对值
public static double abs(double num)
20.3 向上取整(向正方向)
public static double ceil(double num)
20.3 向下取整
public static double floor(double num)
20.4 四舍五入
public static long round(double num)
20.5 Math.PI
- 代表近似的圆周率常量(double)。
20.6 练习
public class DemoMath {
public static void main(String[] args) {
double min = -10.8;
double max = 5.9;
int count = 0;
for (int i = (int) min; i < max; i++) {
int result = Math.abs(i);
if (result > 6 || result < 2.1) {
System.out.print(i + " ");
count++;
}
}
System.out.println();
System.out.println("满足条件的数量是:" + count);
}
}