JavaScript Math对象常用数学方法

 JS 中常用的数学方法:

/*绝对值运算*/
    Math.abs(-5);   //5
    Math.abs(5);   //5

    /*四舍五入运算*/
    Math.round(2.6);   //3
    Math.round(2.4);   //2

    /*向上取整运算*/
    Math.ceil(1.1);   //2
    Math.ceil(1.9);   //2

    /*向下取整运算*/
    Math.floor(1.1);    //1
    Math.floor(1.9);    //1
    
    /*多个数取最大值,可有多个参数*/
    Math.max(x1,x2,x3,...);
    Math.max(-2,5);      //5
    Math.max(2,-5,8);    //8

    /*多个数取最小值,可有多个参数*/
    Math.min(x1,x2,x3,...);
    Math.min(-2,5);     //-2
    Math.min(2,-5,8);   //-5

    /*获取随机数大于等于0小于1*/
    Math.random();      //随机产生[0,1)的数
    
    /*Math的其他方法*/
    Math.cos(x);        //参数是以弧度为单位的数值,返回余弦值
    Math.exp(x);        //参数是数值,返回e的x次方
    Math.log(x);        //参数是数值,返回lnx
    Math.sqrt(x);       //参数是数值,返回平方根
    Math.pow(x,y);      //参数一次是基数、指数,返回x的y次方

猜你喜欢

转载自blog.csdn.net/qq_41261490/article/details/81192365