zoukankan      html  css  js  c++  java
  • javaScript算数对象

    Math 对象

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

    Math 对象提供多种算数值类型和函数。无需在使用这个对象之前对它进行定义。

    算数值

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

    • 常数
    • 圆周率
    • 2 的平方根
    • 1/2 的平方根
    • 2 的自然对数
    • 10 的自然对数
    • 以 2 为底的 e 的对数
    • 以 10 为底的 e 的对数

    这是在 Javascript 中使用这些值的方法:(与上面的算数值一一对应)

    • Math.E
    • Math.PI
    • Math.SQRT2
    • Math.SQRT1_2
    • Math.LN2
    • Math.LN10
    • Math.LOG2E
    • Math.LOG10E

    算数方法

    除了可被 Math 对象访问的算数值以外,还有几个函数(方法)可以使用。

    函数(方法)实例:

    下面的例子使用了 Math 对象的 round 方法对一个数进行四舍五入。

    document.write(Math.round(4.7))

    上面的代码输出为:

    5

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

    document.write(Math.random())

    上面的代码输出为:

    0.9370844220218102

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

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

    上面的代码输出为:

    3

    比较大小,返回大的值

    <html>
    <body>

    <script type="text/javascript">

    document.write(Math.max(5,7) + "<br />")
    document.write(Math.max(-3,5) + "<br />")
    document.write(Math.max(-3,-5) + "<br />")
    document.write(Math.max(7.25,7.30))

    </script>

    </body>

    </html>

    比较大小,返回小的值

    <html>
    <body>

    <script type="text/javascript">

    document.write(Math.min(5,7) + "<br />")
    document.write(Math.min(-3,5) + "<br />")
    document.write(Math.min(-3,-5) + "<br />")
    document.write(Math.min(7.25,7.30))

    </script>

    </body>

    </html>

     
  • 相关阅读:
    Python--day72--ajax简介
    Python基础1 介绍、基本语法
    10-Python-字符编码
    09-Python-集合
    07-Python-字符串操作
    99-Python-深浅拷贝
    06-Python-字典
    05-Python-判断语句
    04-Python-元组
    03-Python-操作列表
  • 原文地址:https://www.cnblogs.com/JSWBK/p/5603364.html
Copyright © 2011-2022 走看看