当前位置: 首页 > 工具软件 > Math.js > 使用案例 >

JavaScript中Math.round、Math.pow、Math.sqrt的作用

锺离逸春
2023-12-01

Math.round()

math.round(x)的作用通俗讲就是对x进行"四舍五入",对0.5要进行判断对待。
math.round()的原理是对传入的参数+0.5之后,再向下取整得到的数就是返回的结果。这里的向下取整是说取比它小的第一个整数或者和它相等的整数
例如:
Math.round(-1.5)的结果是-1.5 + 0.5 再向下取整,即-1.0取整,结果是-1.。
Math.round(-1.4)的结果是 -1.4 + 0.5 即-0.9 向下取整,结果是-1。
同理,Math.round(1.5)即为 1.5 + 0.5 再向下取整,结果是2。

Math.pow()

math.pow(x,y)的作用是取x的y次幂
例如:
math.pow(4,2) 就是(4²)输出的结果为16
math.pow(10,3) 就是(10³)输出的结果为1000

Math.sqrt()

math.sqrt(x)的作用是取x的平方根
例如:
math.sqrt(9) 输出的结果为3
math.sqrt(25) 输出的结果为5

 类似资料: