zoukankan      html  css  js  c++  java
  • 理顺 JavaScript (10) Math 类


    Math 和其他类不同, 它没有建立方法(不能这样使用: new Math()), 它的所有方法都是静态的(都得挂名调用).
    Math.abs;    //绝对值
    Math.max;    //两个数中的大者
    Math.min;    //两个数中的小者
    Math.random; //随机数
    Math.round;  //四舍五入
    Math.ceil;   //上舍入
    Math.floor;  //下舍入
    Math.exp;    //e 的指数
    Math.log;    //自然对数
    Math.pow;    //x 的 y 次方
    Math.sqrt;   //平方根
    Math.sin;    //正弦
    Math.cos;    //余弦
    Math.tan;    //正切
    Math.asin;   //反正弦
    Math.acos;   //反余弦
    Math.atan;   //反正切
    Math.atan2;  //从 X 轴到一个点的角度
    

    Math 类的还有八个常数
    alert(Math.E);       //2.718281828459045  - 自然对数的底数
    alert(Math.LN10);    //2.302585092994046  - 10 的自然对数
    alert(Math.LN2);     //0.6931471805599453 - 2 的自然对数
    alert(Math.LOG10E);  //0.4342944819032518 - 以 10 为底的 e 的对数
    alert(Math.LOG2E);   //1.4426950408889633 - 以 2 为底的 e 的对数
    alert(Math.PI);      //3.141592653589793  - π
    alert(Math.SQRT1_2); //0.7071067811865476 - 2 的平方根除 1
    alert(Math.SQRT2);   //1.4142135623730951 - 2 的平方根
    

    部分测试
    /* 获取 100 以内的随机数 */
    var n1, n2;
    n1 = Math.ceil(Math.random()*100);
    n2 = Math.ceil(Math.random()*100);
    alert(n1); //9
    alert(n2); //80
    
    /* pow */
    alert(Math.pow(2, 3));     // 8
    alert(Math.pow(1.5, 2.4)); // 2.6461778006805154
    
    /* round、ceil、floor*/
    var x = 1.45;
    alert(Math.round(x));  // 1
    alert(Math.ceil(x));   // 2
    alert(Math.floor(x));  // 1
    x = -1.45;
    alert(Math.round(x));  // -1
    alert(Math.ceil(x));   // -1
    alert(Math.floor(x));  // -2
    

  • 相关阅读:
    sql STUFF用法
    关于原型链
    原生js事件绑定
    http常见7种请求
    关于linux的一些常用的指令
    flex布局详解
    html5 新增元素以及css3新特性
    css浮动以及清除
    css 浮动
    计算机网络
  • 原文地址:https://www.cnblogs.com/del/p/1399656.html
Copyright © 2011-2022 走看看