Math对象方法

//返回最大值
    var max=Math.max(95,93,90,94,98);
    console.log(max);
    
    //返回最小值
    var min=Math.min(95,93,90,94,98);
    console.log(min);
    
    //向上取整
     console.log(Math.ceil(2.2));
     console.log(Math.ceil(-2.2));
    
    //向下取整
     console.log(Math.floor(2.2));//2
     console.log(Math.floor(-2.2));//-3
    
    
    //四折五入
     console.log(Math.round(2.4));//四舍五入--3
     console.log(Math.round(-2.5));//负数+0.5,向下取整
     console.log(Math.round(-3.4));//-3
    
    //随机数
     var b=Math.random();//[0,1)
     var d=b*41//[0,41)所有数
     var e=d+10//[10,51)所有数
     var f=Math.floor(e)//[10,50]之间的整数

    //10到50的区间,包含10也包含50
     var gongs=Math.floor(Math.random()*(50-10+1)+10);
     var num=-10;
     Math.abs(num);//10
     Math.abs(10);//10
    
    
    //返回 e 的 x 次幂的值。
     console.log(Math.exp(4))//e
    
    //返回数的自然对数(底为e)
     console.log(Math.log(2))
     
    //pow() 方法可返回 x 的 y 次幂的值
     console.log(Math.pow(2,3))//8
    
    //sqrt() 方法可返回一个数的平方根
     console.log(Math.sqrt(2))//
    
    
    //关于随机数的一个小练习
    //每刷新一次字的颜色就要变化一次
    var num2=9;
    console.log(num2.toString(16))//f,toString转换成字符串
    //一位【0,15】
    var color="#";//用变量进行字符串拼接
    for(var i=0;i<6;i++){//该循环循环6次,获取16进制表示颜色的数
      var yi=Math.floor(Math.random()*16).toString(16);
      color=color+yi;//字符串拼接获取一个完整的颜色的值
    }
    console.log(color);//检测16进制的颜色是否成功合成
    document.write("<font color="+color+">我会变颜色</font>")//把颜色打印出来
    //0-15

这就是一些Math对象方法,希望能帮到大家!!!!!!

以上这篇简单谈谈原生js的math对象就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

点赞(305)

评论列表共有 0 条评论

立即
投稿
返回
顶部