Math基本介绍
- Math类包含用于执行基本数学运算的方法,如初等指数,对数,平方根和三角函数。
- Math类都是静态方法,所以可以直接调用
Math类常见方法应用案例
- abs——绝对值
- pow——求幂
- ceil——向上取整
- floor——向下取整
- round——四舍五入
- sqrt——求开方
- random——求随机数
- max——求两个数的最大值
- min——求两个数的最小值
package com.tao.math_;
public class MathMethod {
public static void main(String[] args) {
int abs = Math.abs(-9);
System.out.println(abs);
double pow = Math.pow(2,3);
System.out.println(pow);
double ceil = Math.ceil(3.0001);
System.out.println(ceil);
double ceil2 = Math.ceil(-3.0001);
System.out.println(ceil2);
double floor = Math.floor(3.0001);
System.out.println(floor);
double floor2 = Math.floor(-3.0001);
System.out.println(floor2);
double round = Math.round(22.222);
System.out.println(round);
double round2 = Math.round(22.55);
System.out.println(round2);
double round3 = Math.round(-22.2);
System.out.println(round3);
double sqrt = Math.sqrt(-9.0);
System.out.println(sqrt);
double sqrt2 = Math.sqrt(9.0);
System.out.println(sqrt2);
for(int i = 0; i < 10; i++){
System.out.println((int)(2 + Math.random() * (7 - 2 + 1)));
}
int max = Math.max(4,5);
int min = Math.min(4,5);
}
}