用于执行基本数学运算的方法!
Math类的所有执行方法都是静态方法,可以直接使用类名.方法名调用
public static void main(String[] args) {
long round = Math.round(1.6);
System.out.println("Math.round(1.6) === "+round);
double rint = Math.rint(2.3);
System.out.println("Math.rint(2.3) === "+rint);
double rint1 = Math.rint(2.6);
System.out.println("Math.rint(2.6) === "+rint1);
int max = Math.max(1, 2);
System.out.println("Math.max(1, 2) === "+max);
double max1 = Math.max(1.25, 1.26);
System.out.println("Math.max(1.25, 1.26) === "+max1);
}
Math.round(1.6) === 2
Math.rint(2.3) === 2.0
Math.rint(2.6) === 3.0
Math.max(1, 2) === 2
Math.max(1.25, 1.26) === 1.26
常用的方法:
Math.round() 返回最接近参数的 int,它表示"四舍五入"
Math.rint() 返回最接近参数并等于某一整数的 double 值,如果有2个数同样接近,则返回偶数的那个
Math.floor() 返回最大的(最接近正无穷大)double 值,该值小于等于参数,并等于某个整数
Math.ceil() 返回最小的(最接近负无穷大)double 值,该值大于等于参数,并等于某个整数
Math.cbrt() 返回 double 值的立方根
Math.sqrt() 返回正确舍入的 double 值的正平方根
Math.pow() 返回第一个参数的第二个参数次幂的值
Math.max() 返回两个 double 值中较大的一个
Math.min() 返回两个 double 值中较小的一个