round【小数点以下四捨五入】
ceil【小数点以下切り上げ】
floor【小数点以下切り下げ】
trunc【数値の整数部分】
abs【絶対値】
sign【数値の符号】
Math.round【小数点以下四捨五入:round off】
メモ
概要
- 小数点以下を四捨五入
- 負の場合も小数部が .5 の場合、切り上げ
関連
外部リンク
- ECMA-262 (英語)
Math.round ( x ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
Math.round( x )
小数点以下を四捨五入した整数値
(引数が特殊な場合は下記参照)
x数値
特殊仕様
x | 戻り値 |
---|---|
数値以外 | 数値に変換して処理 |
NaN | NaN |
+0 | +0 |
-0 | -0 |
+∞ | +∞ |
-∞ | -∞ |
0 < x < 0.5 | +0 |
-0.5 ≦ x < 0 | -0 |
例
console.log(Math.round(1.5));
// 出力:2
console.log(Math.round(1.4));
// 出力:1
console.log(Math.round(-1.5));
// 出力:-1
console.log(Math.round(-1.5000001));
// 出力:-2
console.log(Math.round(-1.6));
// 出力:-2
console.log(Math.round(NaN));
// 出力:NaN
console.log(Math.round(+0));
// 出力:0
console.log(Math.round(-0));
// 出力:-0
console.log(Math.round(+Infinity));
// 出力:Infinity
console.log(Math.round(-Infinity));
// 出力:-Infinity
console.log(Math.round(0.4));
// 出力:0
console.log(Math.round(-0.5));
// 出力:-0
Math.ceil【小数点以下切り上げ:ceiling】
メモ
概要
- 小数点以下を切り上げ
関連
外部リンク
- ECMA-262 (英語)
Math.ceil ( x ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
Math.ceil( x )
小数点以下を切り上げた整数値
(引数が特殊な場合は下記参照)
x数値
特殊仕様
x | 戻り値 |
---|---|
数値以外 | 数値に変換して処理 |
NaN | NaN |
+0 | +0 |
-0 | -0 |
+∞ | +∞ |
-∞ | -∞ |
(-1) < x < 0 | -0 |
例
console.log(Math.ceil(1.1));
// 出力:2
console.log(Math.ceil(-1.9));
// 出力:-1
console.log(Math.ceil(NaN));
// 出力:NaN
console.log(Math.ceil(+0));
// 出力:0
console.log(Math.ceil(-0));
// 出力:-0
console.log(Math.ceil(+Infinity));
// 出力:Infinity
console.log(Math.ceil(-Infinity));
// 出力:-Infinity
Math.floor【小数点以下切り下げ:floor】
メモ
概要
- 小数点以下を切り下げ
関連
外部リンク
- ECMA-262 (英語)
Math.floor ( x ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
Math.floor( x )
小数点以下を切り下げた整数値
(引数が特殊な場合は下記参照)
x数値
特殊仕様
x | 戻り値 |
---|---|
数値以外 | 数値に変換して処理 |
NaN | NaN |
+0 | +0 |
-0 | -0 |
+∞ | +∞ |
-∞ | -∞ |
0 < x < 1 | +0 |
例
console.log(Math.floor(1.9));
// 出力:1
console.log(Math.floor(-1.1));
// 出力:-2
console.log(Math.floor(NaN));
// 出力:NaN
console.log(Math.floor(+0));
// 出力:0
console.log(Math.floor(-0));
// 出力:-0
console.log(Math.floor(+Infinity));
// 出力:Infinity
console.log(Math.floor(-Infinity));
// 出力:-Infinity
Math.trunc【数値の整数部分:truncates】
メモ
概要
- 整数部分の取得
関連
外部リンク
- ECMA-262 (英語)
Math.trunc ( x ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
Math.trunc( x )
整数部分
(引数が特殊な場合は下記参照)
x整数部分を求める数値
特殊仕様
x | 戻り値 |
---|---|
数値以外 | 数値に変換して処理 |
NaN | NaN |
+0 | 0 |
-0 | -0 |
例
console.log( Math.trunc(123.45) );
// 出力:123
console.log( Math.trunc(-123.45) );
// 出力:-123
console.log( Math.trunc(NaN) );
// 出力:NaN
console.log( Math.trunc(+0) );
// 出力:0
console.log( Math.trunc(-0) );
// 出力:-0
console.log( Math.trunc('123.45') );
// 出力:123
console.log( Math.trunc('-123.45') );
// 出力:-123
Math.abs【絶対値:absolute value】
メモ
概要
- 絶対値の取得
関連
外部リンク
- ECMA-262 (英語)
Math.abs ( x ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
Math.abs( x )
絶対値
(引数が特殊な場合は下記参照)
x絶対値を求める数値
特殊仕様
x | 戻り値 |
---|---|
数値以外 | 数値に変換して処理 |
NaN | NaN |
−0 | +0 |
−∞ | +∞ |
例
console.log(Math.abs(+3.5));
// 出力:3.5
console.log(Math.abs(-3.5));
// 出力:3.5
console.log(Math.abs(NaN));
// 出力:NaN
console.log(Math.abs(+0));
// 出力:0
console.log(Math.abs(-0));
// 出力:0
console.log(Math.abs(+Infinity));
// 出力:Infinity
console.log(Math.abs(-Infinity));
// 出力:Infinity
Math.sign【数値の符号】
メモ
概要
- 符号の取得
関連
外部リンク
- ECMA-262 (英語)
Math.sign ( x ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
Math.sign( x )
符号 (1 / -1 / その他)
(引数が特殊な場合は下記参照)
x絶対値を求める数値
特殊仕様
x | 戻り値 |
---|---|
数値以外 | 数値に変換して処理 |
NaN | NaN |
+0 | +0 |
-0 | -0 |
例
console.log( Math.sign(123) );
// 出力:1
console.log( Math.sign(-123) );
// 出力:-1
console.log( Math.sign(NaN) );
// 出力:NaN
console.log( Math.sign(+0) );
// 出力:0
console.log( Math.sign(-0) );
// 出力:-0
console.log( Math.sign('123') );
// 出力:1
console.log( Math.sign('-123') );
// 出力:-1