算数方法
round()
可以进行四舍五入,但仅仅保留一位有效数字(若想保留指定的位数,要用
number中的toFixed的方法!)
使用:
document.write(Math.round(15.76));
则会显示15.8
random()
可以产生随机数。(0到1)(不包含0和1)
结合Math.floor()下取整方法可以获得1到9的随机数。
结合Math.round()方法可以获得1到10的随机数。
ex:
获得1到9的随机数
document.write(Math.floor(Math.random()*10));
获得1到10的随机数
document.write(Math.round(Math.random()*10))
max()
可以返回给定两个数字中的最大值。
min()
可以返回给定的两个数字中的最小值。
算数值
圆周率:Math.PI
根号2:Math.SQRT2
- 1/2 的平方根
- 2 的自然对数
- 10 的自然对数
- 以 2 为底的 e 的对数
- 以 10 为底的 e 的对数
- Math.SQRT1_2
- Math.LN2
- Math.LN10
- Math.LOG2E
- Math.LOG10E