zoukankan      html  css  js  c++  java
  • 原生js的math对象

    Math对象方法

     1 //返回最大值
     2         var max=Math.max(95,93,90,94,98);
     3         console.log(max);
     4         
     5         //返回最小值
     6         var min=Math.min(95,93,90,94,98);
     7         console.log(min);
     8         
     9         //向上取整
    10          console.log(Math.ceil(2.2));
    11          console.log(Math.ceil(-2.2));
    12         
    13         //向下取整
    14          console.log(Math.floor(2.2));//2
    15          console.log(Math.floor(-2.2));//-3
    16         
    17         
    18         //四折五入
    19          console.log(Math.round(2.4));//四舍五入--3
    20          console.log(Math.round(-2.5));//负数+0.5,向下取整
    21          console.log(Math.round(-3.4));//-3
    22         
    23         //随机数
    24          var b=Math.random();//[0,1)
    25          var d=b*41//[0,41)所有数
    26          var e=d+10//[10,51)所有数
    27          var f=Math.floor(e)//[10,50]之间的整数
    28 
    29         //10到50的区间,包含10也包含50
    30          var gongs=Math.floor(Math.random()*(50-10+1)+10);
    31          var num=-10;
    32          Math.abs(num);//10
    33          Math.abs(10);//10
    34         
    35         
    36         //返回 e 的 x 次幂的值。
    37          console.log(Math.exp(4))//e
    38         
    39         //返回数的自然对数(底为e)
    40          console.log(Math.log(2))
    41          
    42         //pow() 方法可返回 x 的 y 次幂的值
    43          console.log(Math.pow(2,3))//8
    44         
    45         //sqrt() 方法可返回一个数的平方根
    46          console.log(Math.sqrt(2))//
    47         
    48         
    49         //关于随机数的一个小练习
    50         //每刷新一次字的颜色就要变化一次
    51         var num2=9;
    52         console.log(num2.toString(16))//f,toString转换成字符串
    53         //一位【0,15】
    54         var color="#";//用变量进行字符串拼接
    55         for(var i=0;i<6;i++){//该循环循环6次,获取16进制表示颜色的数
    56             var yi=Math.floor(Math.random()*16).toString(16);
    57             color=color+yi;//字符串拼接获取一个完整的颜色的值
    58         }
    59         console.log(color);//检测16进制的颜色是否成功合成
    60         document.write("<font color="+color+">我会变颜色</font>")//把颜色打印出来
    61         //0-15

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

  • 相关阅读:
    PAT 1097. Deduplication on a Linked List (链表)
    PAT 1096. Consecutive Factors
    PAT 1095. Cars on Campus
    PAT 1094. The Largest Generation (层级遍历)
    PAT 1093. Count PAT's
    PAT 1092. To Buy or Not to Buy
    PAT 1091. Acute Stroke (bfs)
    CSS:word-wrap/overflow/transition
    node-webkit中的requirejs报错问题:path must be a string error in Require.js
    script加载之defer和async
  • 原文地址:https://www.cnblogs.com/user-5253/p/7082543.html
Copyright © 2011-2022 走看看