美文网首页
JS中Math函数的常用方法

JS中Math函数的常用方法

作者: 南崽 | 来源:发表于2020-02-25 23:08 被阅读0次

Math.abs() 获取绝对值

Math.abs(-12) = 12

Math.ceil() and Math.floor()
向上取整和向下取整

 console.log(Math.ceil(12.03));//13
 console.log(Math.ceil(12.92));//13
 console.log(Math.floor(12.3));//12
 console.log(Math.floor(12.9));//12

Math.round() 四舍五入

Math.round(-16.3) = -16
Math.round(-16.5) = -16
Math.round(-16.51) = -17

注意:正数时,包含5是向上取整,负数时包含5是向下取整

Math.random()
取[0,1)的随机小数

案例1:获取[0,10]的随机整数

console.log(parseInt(Math.random()*10));//未包含10
console.log(parseInt(Math.random()*10+1));//包含10

案例2:获取[n,m]之间的随机整数

Math.round(Math.random()*(m-n)+n);

Math.max() and Math.min()
获取一组数据中的最大值和最小值

console.log(Math.max(10,1,9,100,200,45,78));
console.log(Math.min(10,1,9,100,200,45,78));

Math.PI
获取圆周率π 的值

console.log(Math.PI);

Math.pow()
Math.sqrt()

Math.pow()获取一个值的多少次幂
Math.sqrt()对数值开方

Math.pow(10,2) = 100;
Math.sqrt(100) = 10;
//例子:自己定义一个对象,实现系统的max的方法
    function Mymax() {
      //添加了一个方法
      this.getMax = function () {
        //假设这个数是最大值
        var max = arguments[0];
        for (var i = 0; i < arguments.length; i++) {
          if (max < arguments[i]) {
            max = arguments[i];
          }
        }
        return max;
      };
    }
    // 实例对象
    var my = new Mymax();
    console.log(my.getMax(9, 5, 6, 32));
    console.log(Math.max(9, 5, 6, 32));

相关文章

  • JS中Math函数的常用方法

    Math.abs() 获取绝对值 Math.ceil() and Math.floor()向上取整和向下取整 Ma...

  • JS中Math函数的常用方法

    Math 是数学函数,但又属于对象数据类型 typeof Math => ‘object’console.dir(...

  • js随机数

    js产生随机数通常是使用javascript的Math.random()函数 常用的几种方法: 1.Math.ra...

  • js笔记十二之Math数学函数

    Math中的常用方法 数学函数: 虽然它叫函数,但是它是对象数据类型的typeof Math -> objectM...

  • 004 数学函数、字符和字符串

    4.1 常用的 Math 类方法: 三角函数方法 (trigonometric method ) 指数函数方法 (...

  • Javascript Math对象和Date对象常用方法详解

    Math对象 Math 是js中的一个内置对象, 它为数学常量和数学函数提供了属性和方法,而不是一个函数对象。 属...

  • js基础(2)

    5、Math中的常用方法(10个)6、Date日期操作基础详解以及常用方法(12个) 5、Math中的常用方法(1...

  • js Math函数的一些方法

    ### Math及常用分方法 Math称为数学函数,但是它属于队形类型的 ```javascript typeof...

  • JS之Math

    JS之Math Math本身是一个对象,使用时只需调用其内部的属性和方法即可。 Math中常用的属性: Math....

  • 第八天

    Math数学函数中常用的方法 Math是一个对象数据类型值,在它的堆内存中,存储了很多的内置属性和方法,这些方法一...

网友评论

      本文标题:JS中Math函数的常用方法

      本文链接:https://www.haomeiwen.com/subject/pqacchtx.html