Math.trunc() - JavaScript | MDN

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.

Math.trunc() 関数は、引数として与えた数の小数部の桁を取り除くことによって整数部を返します。

試してみましょう

console.log(Math.trunc(13.37));
// 予想される結果: 13

console.log(Math.trunc(42.84));
// 予想される結果: 42

console.log(Math.trunc(0.123));
// 予想される結果: 0

console.log(Math.trunc(-0.123));
// 予想される結果: -0

構文

引数

x

数値。

返値

x の整数部です。

解説

Math.trunc() の処理は、他の 3 つの Math メソッド、Math.floor()Math.ceil()Math.round() よりも直感的です。引数が正の数か負の数かに関わらず、小数点とその右側の桁を切り詰め(切り捨て)ます。

trunc()Math オブジェクトの静的なメソッドなので、自ら生成した Math オブジェクトのメソッドとしてではなく、常に、Math.trunc() として使用してください (Math オブジェクトにはコンストラクタがありません)。

Math.trunc() の使用

js

Math.trunc(-Infinity); // -Infinity
Math.trunc("-1.123"); // -1
Math.trunc(-0.123); // -0
Math.trunc(-0); // -0
Math.trunc(0); // 0
Math.trunc(0.123); // 0
Math.trunc(13.37); // 13
Math.trunc(42.84); // 42
Math.trunc(Infinity); // Infinity

ビット単位の no-op を使った数値の切り捨て

警告: 無視できないエッジケースがあるため、これは Math.trunc() のポリフィルにはなりません。

ビット演算はオペランドを32ビット整数に変換するため、従来は浮動小数点数を切り捨てるために利用されてきました。一般的なテクニックは以下の通りです。

js

const original = 3.14;
const truncated1 = ~~original; // 二重否定
const truncated2 = original & -1; // Bitwise AND with -1
const truncated3 = original | 0; // Bitwise OR with 0
const truncated4 = original ^ 0; // Bitwise XOR with 0
const truncated5 = original >> 0; // Bitwise shifting by 0

これは本質的に toInt32 であり、 Math.trunc とは異なることに注意してください。値が -231 - 1 < value < 231 (-2147483649 < value < 2147483648) を満たさない場合、変換がオーバーフローしてしまいます。

js

const a = ~~2147483648; // -2147483648
const b = ~~-2147483649; // 2147483647
const c = ~~4294967296; // 0

Math.trunc() の代用として ~~ を使うのは、入力の範囲が 32 ビット整数の範囲に収まっていると確信できる場合だけにしてください。

仕様書

Specification
ECMAScript® 2026 Language Specification
# sec-math.trunc

ブラウザーの互換性

関連情報

Help improve MDN

Learn how to contribute

This page was last modified on by MDN contributors.