CKTIS / JavaScript
Math.trunc: the integer part toward zero
Math.trunc converts its argument to Number and removes its fractional part, rounding toward zero.
Version: ECMAScript 2015 onward; example target: Node.js 24.22.2
Syntax
Math.trunc(x)Math.trunc(x) applies ToNumber and returns the integer part of finite x: 3.9 becomes 3, while -3.9 becomes -3. If the Number is already integral, it is returned unchanged, including preservation of +0 and -0. The result is still a Number, so it does not gain arbitrary precision.
NaN and a missing argument produce NaN, while Infinity and -Infinity are returned unchanged. A numeric string is converted; a Symbol or BigInt throws TypeError, and object conversion can run user code. A negative fraction between -1 and 0 produces -0, detectable with Object.is; the input is already a rounded binary64 value before truncation.
The method has existed since ECMAScript 2015 and is not a constructor. The example converts 9876 milliseconds to 9 whole seconds and demonstrates conversion and -0. Do not substitute parseInt for truncating numbers or bitwise operations that coerce to 32 bits; for exact large integers, use suitable BigInt arithmetic instead of Math.trunc.
Examples
const milliseconds = 9876;
console.log(Math.trunc(milliseconds / 1000));
console.log(Math.trunc(3.9), Math.trunc(-3.9));
console.log(Object.is(Math.trunc(-0.25), -0));
console.log(Math.trunc("12.8"));
try { Math.trunc(1n); } catch (error) { console.log(error instanceof TypeError); }Result
9 3 -3 true 12 true
Math.trunc → whole seconds, string conversion, negative zero, and BigInt TypeError