一.简介
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之间的随机数