javascript Math对象

Math(算数对象)执行常见的算数任务。

1、Math对象的属性

Math.E //自然对数的底数,即常量e的值
Math.LN10 //10的自然对数
Math.LN2 //2的自然对数
Math.LOG2E //已2为底e的对数
Math.LOG10E //已10为底e的对数
Math.PI //π的值
Math.SQRT1_2 //1/2的平方根(即2的平方根的倒数)
Math.SQRT2 //2的平方根

2、min()、max()和apply()方法

  用于确定一组数值中的最小和最大值。

例:

var max = Math.max(1, 2, 3, 4, 5);//5
var min = Math.min(1, 2, 3, 4, 5);//1

var values = [1, 2, 3, 4, 5];
var max = Math.max.apply(Math, values);//5
var min = Math.min.apply(Math, values);//1

3、舍入方法

(1)Math.ceil()执行向上舍入。

(2)Math.floor()执行向下舍入。

(3)Math.round()执行标准舍入,即四舍五入。

例:

console.log(Math.ceil(2.1));//3
console.log(Math.floor(3.9));//3
console.log(Math.round(3.3));//3

4、random()方法

  返回大于等于0小于1的一个随机数。

例:

console.log(Math.floor(Math.random() * 10 + 1));//1到10的值

5、其他方法

Math.abs(num);//返回num的绝对值
Math.exp(num);//返回Math.E的num次幂
Math.log(num);//返回num的自然对数
Math.pow(num, power);//返回num的power次幂
Math.sqrt(num);//返回num的平方根
Math.acos(x);//返回x的反余弦值
...
原文地址:https://www.cnblogs.com/cornlin/p/7572141.html