Math属性与方法

一.简介
Java的Math类封装了很多与数学有关的属性和方法。 

二.举例说明
代码

<script>
Math.sqrt(16)----:4.0
Math.cbrt(8)----:2.0
Math.hypot(3,4)----:5.0
------------------------------------------
Math.pow(3,2)----:9.0
Math.exp(3)----:20.085536923187668
------------------------------------------
Math.max(2.3,4.5)----:15
Math.min(2.3,4.5)----:2.3
------------------------------------------
Math.abs(-10.4)----:10.4
Math.abs(10.1)----:10.1
------------------------------------------
Math.ceil(-10.1)----:-10.0
Math.ceil(10.7)----:11.0
Math.ceil(-0.7)----:-0.0
Math.ceil(0.0)----:0.0
Math.ceil(-0.0)----:-0.0
Math.ceil(-1.7)----:-1.0
------------------------------------------
Math.floor(-10.1)----:-11.0
Math.floor(10.7)----:10.0
Math.floor(-0.7)----:-1.0
Math.floor(0.0)----:0.0
Math.floor(-0.0)----:-0.0
------------------------------------------
Math.random()----:0.8979626325354049
Math.random()*100----:32.783762836248144
------------------------------------------
Math.rint(10.1)----:10.0
Math.rint(10.7)----:11.0
Math.rint(-10.5)----:-10.0
Math.rint(-10.51)----:-11.0
Math.rint(-10.2)----:-10.0
Math.rint(9)----:9.0
------------------------------------------
Math.round(10.1)----:10
Math.round(10.7)----:11
Math.round(-10.5)----:-10
Math.round(-10.51)----:-11
Math.round(-10.2)----:-10
Math.round(9)----:9
------------------------------------------
Math.nextUp(1.2)----:1.2000000000000002
Math.nextDown(1.2)----:1.1999999999999997
Math.nextAfter(1.2, 2.7)----:1.2000000000000002
Math.nextAfter(1.2, -1)----:1.1999999999999997
<script/>

结果:

<script>
Math.sqrt(16)----:4.0
Math.cbrt(8)----:2.0
Math.hypot(3,4)----:5.0
------------------------------------------
Math.pow(3,2)----:9.0
Math.exp(3)----:20.085536923187668
------------------------------------------
Math.max(2.3,4.5)----:15
Math.min(2.3,4.5)----:2.3
------------------------------------------
Math.abs(-10.4)----:10.4
Math.abs(10.1)----:10.1
------------------------------------------
Math.ceil(-10.1)----:-10.0
Math.ceil(10.7)----:11.0
Math.ceil(-0.7)----:-0.0
Math.ceil(0.0)----:0.0
Math.ceil(-0.0)----:-0.0
Math.ceil(-1.7)----:-1.0
------------------------------------------
Math.floor(-10.1)----:-11.0
Math.floor(10.7)----:10.0
Math.floor(-0.7)----:-1.0
Math.floor(0.0)----:0.0
Math.floor(-0.0)----:-0.0
------------------------------------------
Math.random()----:0.8979626325354049
Math.random()*100----:32.783762836248144
------------------------------------------
Math.rint(10.1)----:10.0
Math.rint(10.7)----:11.0
Math.rint(-10.5)----:-10.0
Math.rint(-10.51)----:-11.0
Math.rint(-10.2)----:-10.0
Math.rint(9)----:9.0
------------------------------------------
Math.round(10.1)----:10
Math.round(10.7)----:11
Math.round(-10.5)----:-10
Math.round(-10.51)----:-11
Math.round(-10.2)----:-10
Math.round(9)----:9
------------------------------------------
Math.nextUp(1.2)----:1.2000000000000002
Math.nextDown(1.2)----:1.1999999999999997
Math.nextAfter(1.2, 2.7)----:1.2000000000000002
Math.nextAfter(1.2, -1)----:1.1999999999999997

<script/>
       	<script type="text/javascript">
		   document.write(Math.abs(-345) + "<BR>"); // 绝对值
		   document.write(Math.ceil(8.1) + "<BR>"); // 上舍入:比所求数的绝对值大一
		   document.write(Math.floor(8.9999) + "<BR>"); // 下舍入:比所求数的绝对值小一
		   document.write(Math.round(6) + "<BR>"); // 四舍五入
		   document.write(Math.max(666, 777) + "<BR>"); // 求最大数
		   document.write(Math.min(666, 777) + "<BR>"); // 求最小数
		   document.write(Math.sqrt(5) + "<BR>");
		   document.write(Math.random() + "<BR>"); // 返回0~1之间的随机数
		  </script>

总结:

Math.abs() -- 返回数字的绝对值

Math.ceil() -- 返回大于等于数字参数的最小整数(取整函数),对数字进行上舍入

Math.floor() -- 返回小于等于数字参数的最大整数,对数字进行下舍入

Math.round() -- 返回数字最接近的整数,四舍五入

Math.max(x,y) -- 返回数个数字中较大的值

Math.min(x,y) -- 返回数个数字中较小的值

Math.random() -- 返回0和1之间的随机数

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值