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()方法

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

1 let max=Math.max(1,2,3,4,2,22,99); // 99
2 let min=Math.min(1,2,3,4,2,22,99); // 1
3 let arr = [1,2,22,33,99];
4 max=Math.max.apply(Math,arr); //99
5 min=Math.min.apply(Math,arr); // 1

3.舍入方法

ceil()执行向上舍入,将数值向上舍入最接近的整数
floor()执行向下舍入,将数值向下舍入最接近的整数
round()四舍五入
 1 Math.ceil(99.1); // 100
 2 Math.ceil(99.9); // 100
 3 Math.ceil(99.5); // 100
 4 
 5 Math.floor(99.5); // 99
 6 Math.floor(99.1); // 99
 7 Math.floor(99.9); // 99
 8 
 9 Math.round(99.9); // 100
10 Math.round(99.1); // 99
11 Math.round(99.5); // 100

4.random()方法

从某个整数范围内随机选择一个值

值 = Math.floor(Math.random()*可能值的总数+第一个可能值)
1 //1-10的整数
2 Math.floor(Math.random()*10+1) 
3 
4 //2-10的整数
5 Math.floor(Math.random()*10+2) 

5.其他方法

方法 说明
Math.abs(num) 返回num的绝对值
Math.exp(num) 返回Math.E的num次幂
Math.log2(num) 返回一个数字以 2 为底的对数
Math.log(num) 返回num的自然对数
Math.log10(num) 返回一个数字以 10 为底的对数
Math.pow(num,power); 返回num的power次幂
Math.sqrt(num) 返回num的平方根
Math.acos(x) 返回x的反余弦
Math.asin(x) 返回x的反正弦
Math.atan(x) 返回x的反正切
Math.cos(x) 返回x的余弦
Math.sin(x) 返回x的正弦
Math.tan(x) 返回x的正切
posted @ 2017-12-18 16:07  biubiu小希希  阅读(216)  评论(0编辑  收藏  举报