[JavaScript] abs、sqrt、floor、trunc、random、round、acos、asin などの JavaScript Math オブジェクトの一般的なメソッドの完全なコレクション (関数、構文、パラメーター分析、詳細な例を含む)

1. abs() メソッド

機能: 数値の絶対値を返します。x が数値でない場合は NaN を返し、x が null の場合は 0 を返します。
文法:Math.abs(x)

パラメータ 説明する
バツ 必須。数値である必要があります。

例:

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

2. acos() メソッド

機能: 数値の逆余弦を返すことができます。パラメータ x が -1.0 ~ 1.0 の範囲を超える場合、ブラウザは NaN を返します。パラメータ x の値が -1 の場合、PI が返されます。
構文:Math.acos(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)

パラメータ 説明する
バツ 必須。-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)

パラメータ 説明する
バツ 必須。数値である必要があります。

例:

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 座標を表す数値
y しなければならない。y 座標を表す数値

例:

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

6. ceil()メソッド

機能: 数値を切り上げることができます。
文法:Math.ceil(x)

パラメータ 説明する
バツ 必須。数値である必要があります。

例:

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

7. cos()メソッド

機能: 数値のコサインを返します。cos() は、-1.0 から 1.0 までの数値を返します。
文法:Math.cos(x)

パラメータ 説明する
バツ 必須。数値である必要があります。

例:

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

8.exp()メソッド

機能: e の x 乗の値を返します。E は天然塩基 (約 2.7183) です。
文法:Math.exp(x)

パラメータ 説明する
バツ eのx乗

例:

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

9.floor() 方法

機能: x 以下の最大の整数を返します。
文法:Math.floor(x)

パラメータ 説明する
バツ 必須。任意の数値または式。

例:

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

10. log()メソッド

機能: 数値の自然対数 (E に基づく) を返します。x が負の場合、NaN を返します。x が 0 の場合、-Infinity を返します。
文法:Math.log(x)

パラメータ 説明する
バツ 必須。任意の数値または式。

例:

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

11. max() メソッド

機能: 指定された 2 つ以上の数値のうち、大きい方の数値を返します。
文法: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() メソッド

機能: 指定された 2 つ以上の数値のうち、小さい方の数値を返します。
文法: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)

パラメータ 説明する
バツ 必須。ベース番号。数値である必要があります。
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)

パラメータ 説明する
バツ 必須。ラジアンで表される角度。角度に 0.017453293 (2PI/360) を掛けてラジアンに変換します。

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

17. sqrt()メソッド

関数: 数値の平方根を返します。 構文
:Math.sqrt(x)

パラメータ 説明する
バツ 必須。0 以上の数値である必要があります。

例:

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

18.tan()メソッド

機能: 角度の正接を表す数値を返します。
文法:Math.tan(x)

パラメータ 説明する
バツ 必須。ラジアンで表される角度。角度に 0.017453293 (2PI/360) を掛けてラジアンに変換します。

例:

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

19.tanh()メソッド

関数: 数値の双曲線正接関数の値を返します。 構文
:Math.tanh(x)

パラメータ 説明する
バツ 必須。計算される数値。

例:

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

20.trunc()メソッド

機能: 数値の小数部分を削除し、整数部分のみを保持します
。 構文:Math.trunc(x)

パラメータ 説明する
バツ いずれかの番号

例:

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

結論は

  この記事では主に JavaScript Math オブジェクトの一般的に使用されるメソッドをいくつかまとめています。間違いや不完全な点があれば、アドバイスをお願いします。

おすすめ

転載: blog.csdn.net/m0_46533551/article/details/129361988
おすすめ