js中Math方法

Math方法

1. Math.round(): 四舍五入

Math.round(2.5); // 3
Math.round(2.4); // 2

2. Math.random(): 0 ~ 1 之间的随机数

Math.random(); //0.5214744192115168
Math.random();//0.010620681698898515

这个方法很多中可以拓展的新方法:

  • 可以随机取得0-10数:
parseInt(Math.random() * 10)//8(随机);

  • 可以取得随机十六位进制:
parseInt(Math.random() * 16).toString(16);//'f'
  • 可以取得随机颜色
 //方法一
 function getRandomColor(){
        let str = '0123456789ABCDEF';
        let randomColor = '#'
        for(let i = 0; i< 6; i++){
             let randomNum = Math.floor(Math.random() * 16);
            randomColor += (str.split('')[randomNum])
        }
        return randomColor
    }
    
    //方法二
    function generateRandomColor() {
		  // 生成 0-255 范围的三个随机数,转换为十六进制格式,并用 2 位补全
		  const randomColor = '#' + 
		    Math.floor(Math.random() * 256).toString(16).padStart(2, '0') +  // 红色
		    Math.floor(Math.random() * 256).toString(16).padStart(2, '0') +  // 绿色
		    Math.floor(Math.random() * 256).toString(16).padStart(2, '0');  // 蓝色
		  return randomColor;
		}

还有很多种方便的用法这里就不一一列举了

3. Math.abs(x): 取一个数的绝对值

Math.abs(1) //1
Math.abs(-1) //1
Math.abs(null); //0
Math.abs(''); //0
Math.abs('3');// 3
Math.abs("Hello"); //NaN

4. Math.floor(x) : 向下取整 返回小于等于x的最大整数

Math.floor(1.9); //1
Math.floor(-1.1); // -2
Math.floor(null); //0
Math.floor('g') //NaN

5. Math.ceil(x): 向上取整 对一个数进行上舍入:

Math.ceil(1.4) //2
Math.ceil(-1.9)// -1
Math.ceil(-Infinity) //-Infinity

6. Math.max(x1, x2, ….xn): 返回最大值

Math.max(1, 2) //2
Math.max(1, 2, 5, 6,77,7345,435, -1, 2) //7345
Math.max(1, 2, 5, 6,77,7345,435, -1, 2, Infinity,  -Infinity) //Infinity

7. Math.min(x1, x2, ….xn): 返回最小值

Math.min(1, 2) //1
Math.min(1, 2, 5, 6,77,7345,435, -1, 2, Infinity, -Infinity)//-Infinity
Math.min(-1, 0,5) //-1
Math.min(-1, 'g') // 

8. Math.pow(x, y): 方法返回 x 的 y 次幂。

Math.pow(4,3); // 4*4*4 = 64
Math.pow(4,Infinity); //Infinity
Math.pow(4,-Infinity);//0

9. Math.sqrt(x): 返回一个数的平方根

Math.sqrt(9); //3
Math.sqrt(-9); // NaN
Math.sqrt(Infinity); //Infinity

10. Math.log(x): 返回数的自然对数(底为e)

Math.log(2) //0.6931471805599453
Math.log(0) //-Infinity
Math.log(-1) // NaN

11. Math.exp(x): 返回 e 的 x 次幂的值  E 为自然底数 (近似值 2.7183)。

Math.exp(1) //2.718281828459045

12. Math.sin(x): 返回x的正弦值

Math.sin(3); //0.1411200080598672
Math.sin(-3); //-0.1411200080598672
Math.sin(Math.PI) //1.2246467991473532e-16
Math.sin(Math.PI/2)// 1

13. Math.PI: 返回圆周率 3.141592653589793…

Math.PI //3.141592653589793

14. Math.cos(x): 返回x的余弦值

Math.cos(Math.PI); //-1
Math.cos(Math.PI * 2); // 1

15. Math.tan(x): 返回x的正切值

Math.tan(90);//-1.995200412208242
Math.tan(45);//1.6197751905438615

16. Math.asin(x): 返回x的反正弦值

Math.asin(0.5); //0.5235987755982989

17. Math.acos(x): 返回 x 的反余弦值。

Math.acos(0.5)//1.0471975511965979

18. Math.atan(x): 以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值

Math.atan(2);//1.1071487177940904

19. Math.atan2(y,x): 返回从 x 轴到点 (x,y) 的角度(介于 -PI/2 与 PI/2 弧度之间)

它求的是和x轴正方向的夹角,x轴正方向为0,顺时针为负值,逆时针为正值,顺逆时针(判定为优弧所在范围), 取值范围(-PI, PI]

Math.atan2(4,3)//0.9272952180016122

20. Math.tanh(x): 返回x的双曲正切函数值

Math.tanh(1);//0.7615941559557649
Math.tanh(Infinity); // 1
Math.tanh(0); //0

21. Math.trunc(x.xx): 将数字的小数部分去掉:

Math.trunc(3.1)// 3
Math.trunc(3)// 3
Math.trunc(0) //0
Math.trunc(0.123)    //  0
Math.trunc(-0.123)   // -0
Math.trunc("-1.123") // -1
Math.trunc(NaN)      // NaN
Math.trunc("foo")    // NaN
Math.trunc()         // NaN

22. Math.E: 返回算术常量 e,即自然对数的底数(约等于2.718)

Math.E //2.718281828459045

23. Math.LN2: 返回 2 的自然对数(约等于0.693)。

Math.LN2//0.6931471805599453

24. Math.LN10: 返回 10 的自然对数(约等于2.302)

Math.LN10 //2.302585092994046

25. Math.LOG2E : 返回以 2 为底的 e 的对数(约等于 1.4426950408889634)

Math.LOG2E //0.4342944819032518

26. Math.LOG10E: 返回以 10 为底的 e 的对数(约等于0.434)

Math.LOG10E //0.4342944819032518

27. Math.SQRT1_2: 返回 2 的平方根的倒数(约等于 0.707)

Math.SQRT1_2 //0.7071067811865476

28. Math.SQRT2: 返回 2 的平方根(约等于 1.414)

Math.SQRT2 //1.4142135623730951
posted @ 2024-12-05 09:20  heshanwan  阅读(76)  评论(0)    收藏  举报