轻松掌握JavaScript中的Math object数学对象
2016-06-20来源:易贤网

Math对象或者说是类型旗下拥有很多平时常用的数学函数,虽然并不像Date和String那样拥有类似于类和方法那样的使用方式,不过并不妨碍您通过本文来轻松掌握JavaScript中的Math object数学对象^^

对于内置的Math数学常项和函数也有一些属性和方法。 比方说, Math对象的 PI 属性会有属性值 pi (3.141...),你可以像这样调用它:

Math.PI

同理,标准数学函数也是Math的方法。 这些包括三角函数,对数,指数,和其他函数。比方说你想使用三角函数 sin, 你可以这么写:

Math.sin(1.56)

需要注意的是Math的所有三角函数参数都是弧度制。

和其他对象不同,你不能够创建一个自己的Math对象。你只能使用内置的Math对象。

eg:

1.min( )和max( )

var value = [1,2,3,4,5,6,7,8];

var max = Math.max.apply(Math, values);

2.舍入方法

Math.ceil( ):向上舍入

Math.floor( ):向下舍入

Math.round( ):四舍五入

3.random( )

Math.random( )方法返回介于0和1之间的一个随机数,不包括0和1

var num = Math.floor(Math.random()*10, + 1)//返回1-10之间的数

4.round()

如何使用 round()。

<html>

<body>

<script type="text/javascript">

document.write(Math.round(0.60) + "<br />")

document.write(Math.round(0.50) + "<br />")

document.write(Math.round(0.49) + "<br />")

document.write(Math.round(-4.40) + "<br />")

document.write(Math.round(-4.60))

</script>

</body>

</html>

5.random()

如何使用 random() 来返回 0 到 1 之间的随机数。

<html>

<body>

<script type="text/javascript">

document.write(Math.random())

</script>

</body>

</html>

2026公务员·事业单位培训课程试听报名

  • 报班类型
  • 姓名
  • 手机号
  • 验证码
推荐信息