【Java-----Math函数的常用方法】

Math函数的常用方法

Math.sqrt(a):计算a的平方根

 System.out.println(Math.sqrt(16));
//输出:4.0

Math.cbrt(a):计算a的立方根

System.out.println(Math.cbrt(27));
//输出:3.0

Math.pow(a,b):计算a的b次方

System.out.println(Math.pow(2,3));
//输出:8.0

Math.max(a,b):计算最大值

System.out.println(Math.max(9,34));
//输出:34

Math.min(a,b):计算最小值

System.out.println(Math.min(9,34));
//输出:9

Math.abs(a):取a的绝对值

System.out.println(Math.abs(-28));
//输出:28

Math.ceil(a):对a向上取整

System.out.println(Math.ceil(2.09));
//输出:3.0

Math.floor(a):对a向下取整

System.out.println(Math.floor(4.98));
//输出:4.0

Math.rint(a):四舍五入,返回double值,注意小数点后面是.5的时候会取偶数

System.out.println(Math.rint(4.98));//5.0
System.out.println(Math.rint(3.5));//4.0
System.out.println(Math.rint(6.5));//6.0
System.out.println(Math.rint(1.9));//2.0
System.out.println(Math.rint(8.2));//8.0

Math.round(a):四舍五入,float时返回int值,double时返回long值

System.out.println(Math.round(2.8));//3
System.out.println(Math.round(3.5));//4
System.out.println(Math.round(6.5));//7
System.out.println(Math.round(1.16));//1
System.out.println(Math.round(9.2));//9

Math.random():取得一个[0,1)范围内的随机数

//[0,1)的double类型的数
System.out.println(Math.random());
//[0,100)的double类型的数
System.out.println(Math.random()*100);
//[10,20)的double类型的数
System.out.println(Math.random()*10+10);

Math.sin(a):返回角度的三角正弦值,角度以弧度表示(360度=2π弧度)

double pi=Math.PI;
System.out.println(Math.sin(0));//0.0
System.out.println(Math.sin(pi/2));//1.0
System.out.println(Math.sin(pi/6));//0.49999999999999994

Math.cos(a):返回角度的三角余弦值,角度以弧度表示

double pi=Math.PI;
System.out.println(Math.cos(0));//1.0
System.out.println(Math.cos(pi));//-1.0
System.out.println(Math.cos(pi/3));//0.5000000000000001

Math.tan(a):返回角度的三角正切值,角度以弧度表示

double pi=Math.PI;
System.out.println(Math.tan(0));//0.0
System.out.println(Math.tan(1));//1.5574077246549023
System.out.println(Math.tan(pi/3));//1.7320508075688767

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值