JavaScript 中的Math,算数对象

Math(算数)对象的作用是:执行常见的算数任务。

JavaScript 提供 8 种可被 Math 对象访问的算数值:

  • 常数 ========>Math.E
  • 圆周率 =============>Math.PI
  • 2 的平方根 ===========>Math.SQRT2
  • 1/2 的平方根 ==========>Math.SQRT1_2
  • 2 的自然对数 ==========>Math.LN2
  • 10 的自然对数 ==========>Math.LN10
  • 以 2 为底的 e 的对数 ======>Math.LOG2E
  • 以 10 为底的 e 的对数======>Math.LOG10E

    第一:使用了 Math 对象的 round() 方法对一个数进行四舍五入。

    document.write(Math.round(4.7)); //输出为5.

    第二:使用了 Math 对象的random() 方法来返回一个介于 0 和 1 之间的随机数。

    document.write(Math.random());

    第三:使用了 Math 对象的 floor() 方法和 random() 来返回一个介于 0 和 10 之间的随机数。

    document.write(Math.floor(Math.random()*11));

    第四:使用 max() 来返回两个给定的数中的较大的数。

    document.write(Math.max(5,7));

    第五:使用 min() 来返回两个给定的数中的较小的数。

    document.write(Math.min(5,7));