在java中,我们要想对数据进行取绝对值,四舍五入,求最大值,最小值等操作时,一般会用到Math中的所对应的方法以较为方便的达成自己所需目的,因此Math类是每个java程序员必须掌握的知识。
首先,Math类是一个工具类,我们无法创建Math的对象,在使用Math中的方法时,可以直接通过类名调用,还是比较方便的。接下来是Math类中一些比较常用的函数。
1: abs函数求绝对值:int abs(int a),需向方法传递int类型整数,返回值为int类型。例如:Math.abs(-10)=10,Math.abs(10)=10;
2: ceil函数向上取整:double ceil(double a),需向方法传递double类型浮点数,返回值为double类型,例如:Math.ceil(-10.5)=-10.0, Math.ceil(10.5)=11.0。
3: floor函数向下取整:double floor(double a),需向方法传递double类型浮点数,返回值为double类型,例如:Math.floor(-10.5)=-11.0, Math.floor(10.5)=10.0;
4: max,min函数取两个数中的最大最小值。
5:round函数对数据进行四舍五入操作:long round(double a),例如Math.round(4.7)=5,Math.round(-10.1)=-10;
6: sqrt函数对数据求平方根:double sqrt(double a),例如Math.sqrt(36)=6;
7: pow函数进行幂运算:double pow(double a,double b),例如Math.pow(2,3)=8;