Math类
java.lang.Math 提供了一系列静态方法用于科学计算,常用方法如下:
abs 绝对值
acos,asin,atan,cos,sin,tan 三角函数
sqrt 平方根
pow(double a,double b) a的b次幂
max(double a,double b) 取大值
min(double a,double b) 取小值
ceil(double a) 大于a的最小整数
floor(double a) 小于a的最大整数
random() 返回 0.0 到 1.0 的随机数
long round(double a) double 型的数据 a 转换为 long 型(四舍五入)
toDegrees(double angrad) 弧度->角度
toRadians(double angdeg) 角度->弧度
Math类的常用方法:
public class TestMath {public static void main(String[] args){//取整相关操作System.out.println(Math.ceil(3,2));System.out.println(Math.floor(3,2));System.out.println(Math.round(3,2));System.out.println(Math.round(3,8));//绝对值、开方、a的b次幂等操作System.out.println(Math.abs(-45));System.out.println(Math.sqrt(64));System.out.println(Math.pow(5,2));System.out.println(Math.pow(2,5));//Math类中常用的常量System.out.println(Math.PI);System.out.println(Math.E);//随机数System.out.println(Math.random()); //[0,1)}
}
执行结果如图所示: