JavaScript,Math,数学对于内置的Math数学常项和函数也有一些属性和方法。 比方说, Math对象的 PI 属性会有属性值 pi (3.141...),你可以像这样调用它:
同理,标准数学函数也是Math的方法。 这些包括三角函数,对数,指数,和其他函数。比方说你想使用三角函数 sin, 你可以这么写:
需要注意的是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> JavaScript,Math,数学
|