【JavaScript】JavaScript Math对象常用方法大全,例如abs、sqrt、floor、trunc、random、round、acos、asin(包括作用、语法、参数解析、详细例子)

1.abs() 方法

作用:返回一个数的绝对值。如果 x 不是数字返回 NaN,如果 x 为 null 返回 0。
语法:Math.abs(x)

参数 描述
x 必需。必须是一个数值。

例子:

let test=Math.abs(-1)
console.log(test); //输出结果:1

2.acos() 方法

作用:可返回一个数的反余弦。如果参数 x 超过了 -1.0 ~ 1.0 的范围,那么浏览器将返回 NaN。如果参数 x 取值 -1,那么将返回 PI。
语法:Math.acos(x)
例子:

参数 描述
x 必需。必须是 -1.0 ~ 1.0 之间的数

例子:

let test=Math.acos(0.5);
console.log(test);//输出结果:1.0471975511965979

3.asin() 方法

作用:返回-PI/2 到 PI/2 之间的弧度值。如果参数 x 超过了 -1.0 ~ 1.0 的范围,那么浏览器将返回 NaN。如果参数 x 取值 1,那么将返回 PI/2。
语法:Math.asin(x)

参数 描述
x 必需。必须是一个数值,该值介于 -1.0 ~ 1.0 之间。

例子:

let test=Math.asin(0.5);
console.log(test);//输出结果:0.5235987755982989

4.atan() 方法

作用:以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值。
语法:Math.atan(x)

参数 描述
x 必需。必须是一个数值。

例子:

let test=Math.atan(2);
console.log(test);  //输出结果:1.1071487177940904

5.atan2() 方法

作用:返回从原点(0,0) 到 (x,y) 点的线段与 x 轴正方向之间的平面角度(弧度值),也就是 Math.atan2(y,x)。
语法:Math.atan2(y,x)

参数 描述
x 必须。 一个数字代表x坐标
y 必须。 一个数字代表y坐标

例子:

let test=Math.atan2(1,1);
console.log(test);  //输出结果:0.7853981633974483

6.ceil() 方法

作用:可对一个数进行上舍入。
语法:Math.ceil(x)

参数 描述
x 必需。必须是一个数值。

例子:

let test=Math.ceil(5.1);
console.log(test); //输出结果:6

7.cos() 方法

作用:返回一个数字的余弦值。cos() 返回的是 -1.0 到 1.0 之间的数。
语法:Math.cos(x)

参数 描述
x 必需。必须是一个数值。

例子:

let test=Math.cos(Math.PI);
console.log(test); //输出结果:-1

8.exp() 方法

作用:返回 e 的 x 次幂的值。 E 为自然底数 (近似值 2.7183)。
语法:Math.exp(x)

参数 描述
x e的x次幂

例子:

let test=Math.exp(1);
console.log(test); //输出结果:2.718281828459045

9.floor() 方法

作用:返回小于等于x的最大整数。
语法:Math.floor(x)

参数 描述
x 必需。任意数值或表达式。

例子:

let test=Math.floor()(1.2);
console.log(test);  //输出结果:1 

10.log() 方法

作用:返回一个数的自然对数(基于E)。如果 x 为负数,返回 NaN。 如果 x 为0,返回 -Infinity 。
语法:Math.log(x)

参数 描述
x 必需。任意数值或表达式。

例子:

let test=Math.log(Math.PI);
console.log(test);  //输出结果:1.1447298858494002

11.max() 方法

作用:返回两个或多个指定的数中带有较大的值的那个数。
语法:Math.max(n1,n2,n3,...,nX)

参数 描述
n1,n2,n3,…,nX 可选。1 或多个值。

例子:

let test=Math.max(-1,0,-2,3,2);
console.log(test);  //输出结果:3

12.min() 方法

作用:返回两个或多个指定的数中带有较小的值的那个数。
语法:Math.min(n1,n2,n3,...,nX)

参数 描述
n1,n2,n3,…,nX 可选。1 或多个值。

例子:

let test=Math.min(-1,0,-2,3,2);
console.log(test);  //输出结果:-2

13.pow() 方法

作用:返回 x 的 y 次幂。
语法:Math.pow(x,y)

参数 描述
x 必需。底数。必须是数字。
y 必需。幂数。必须是数字。

例子:

let test=Math.pow(2,3);
console.log(test); //输出结果:8

14.random() 方法

作用:返回介于 0(包含) ~ 1(不包含) 之间的一个随机数。
语法:Math.random()
例子:

let test=Math.random(2,3);
console.log(test);  

15.round() 方法

作用:把一个数字舍入为最接近的整数
语法:Math.round(x)
例子:

let test=Math.round(2.1);
console.log(test);  //输出结果:2

16.sin() 方法

作用:返回参数 x 的正弦值。返回值在 -1.0 到 1.0 之间
语法:Math.sin(x)

参数 描述
x 必需。一个以弧度表示的角。将角度乘以 0.017453293 (2PI/360)即可转换为弧度。

例子

let test=Math.sin(2);
console.log(test);  //输出结果:0.9092974268256817

17.sqrt() 方法

作用:返回一个数的平方根
语法:Math.sqrt(x)

参数 描述
x 必需。必须是大于等于 0 的数。

例子:

let test=Math.sqrt(4);
console.log(test);  //输出结果:2

18.tan() 方法

作用:返回一个表示某个角的正切的数字。
语法:Math.tan(x)

参数 描述
x 必需。一个以弧度表示的角。将角度乘以 0.017453293 (2PI/360)即可转换为弧度。

例子:

let test=Math.tan(90);
console.log(test);  //输出结果:1.995200412208242

19.tanh() 方法

作用:返回一个数的双曲正切函数值
语法:Math.tanh(x)

参数 描述
x 必需。待计算的数字。

例子:

let test=Math.tanh(1);
console.log(test); //输出结果:0.7615941559557649

20.trunc() 方法

作用:将数字的小数部分去掉,只保留整数部分
语法:Math.trunc(x)

参数 描述
x 任意数字

例子:

 let test=Math.trunc(1.111);
console.log(test);  //输出结果:1

结论

  这篇文章主要总结了JavaScript Math对象常用的一些方法,如果哪里有错误或者不完善的地方,欢迎各位指教,谢谢!

猜你喜欢

转载自blog.csdn.net/m0_46533551/article/details/129361988
今日推荐