Math中常用的方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_36282409/article/details/87388183

Math中常用的方法:
1.Math是数学函数,但是它是对象数据类型的;

typeof Math
“object”

2.Math对象中给我提供了很多常用操作数字的方法;

console.dir(Math) 查看Math所有方法

eg: 常用的有:
1.abs
2. ceil/floor
3. round
4. random
5. max/min
6. PI
7.pow/sqrt

2.1 abs方法

Math.abs 取绝对值
…javascript
Math.abs(12) —>12
Math.abs(-12) —>12

2.2 ceil && floor

Math.ceil 向上取整
Math.floor 向下取整
…javascript
Math.ceil(15) —>15
Math.ceil(15.1) —>16
Math.ceil(15.9) —>16
Math.ceil(-15) —>15
Math.ceil(-15.1) —>15
Math.ceil(-15.9) —>15
…javascript
Math.floor(15) —>15
Math.floor(15.1) —>15
Math.floor(15.9) —>15
Math.floor(-15) —>15
Math.floor(-15.1) —>16
Math.floor(-15.9) —>16

2.3 round

Math.round 四舍五入
…javascript
Math.round(12.3) —>12
Math.round(12.5) —>13 正数中,5包含向上;
Math.round(-12.3) —>-12
Math.round(-12.5) —>-12 负数中,5包含在向下;
Math.round(-12.51) —>-13

2.4 random

Math.random 获取[0,1)之间的随机小数
…javascript
for(var i=0;i<100;i++){
console.log(Math.random())
}
//需求:
1.获取[0,10]之间的随机整数
Math.round(Math.random()*10)
2.获取[3,15]之间的随机整数
Math.round(Math.random()12+3)
获取[n,m]之间的随机整数
Math.round(Math.random()
(m-n)+n)

2.5 max&&min

Math.max 获取一组值中的最大值
Math.min 获取一组值中的最小值
…javascript
Math.max(12,14,30,40,23,11,56,12) —>56
Math.max(12,14,30,40,23,11,56,11) —>11

2.6 PI

Math.PI 获取圆周率(π)
…javascript
Math.PI

2.6 pow&&sqrt

Math.pow 获取一个值的多少次幂
Math.sqrt 开平方
…javascript
math.pow(10,2) —>100
math.sqrt(100) —>10

猜你喜欢

转载自blog.csdn.net/weixin_36282409/article/details/87388183