How to Get the Integer Part of a Decimal Number in JavaScript?

In JavaScript, you can return the integer part of a number by removing any fractional digits in the following ways:

Using Math.trunc()

Introduced in ES6, Math.trunc() returns the truncated integer part of a number (which in case of a decimal number would remove the fractional digits, and return only the integer part). To illustrate this, let's look at the following examples:

// ES6+
console.log(Math.trunc(1234.567)); // 1234
console.log(Math.trunc(-1234.567)); // -1234

console.log(Math.trunc(1234)); // 1234
console.log(Math.trunc(-1234)); // -1234

console.log(Math.trunc(Infinity)); // Infinity
console.log(Math.trunc(-Infinity)); // -Infinity

console.log(Math.trunc(100000000000000000000000000000000.2345)); // 1e+32
console.log(Math.trunc(-100000000000000000000000000000000.2345)); // -1e+32

Using Math.floor() and Math.ceil()

To achieve the same effect as with Math.trunc(), you can use Math.floor() to round down positive integers, and Math.ceil() to round up negative integers. For example:

const trunc = (num) => num > 0 ? Math.floor(num) : Math.ceil(num);

console.log(trunc(1234.567)); // 1234
console.log(trunc(-1234.567)); // -1234

console.log(trunc(1234)); // 1234
console.log(trunc(-1234)); // -1234

console.log(trunc(Infinity)); // Infinity
console.log(trunc(-Infinity)); // -Infinity

console.log(trunc(100000000000000000000000000000000.2345)); // 1e+32
console.log(trunc(-100000000000000000000000000000000.2345)); // -1e+32

This method may be useful in cases where you don't have support for ES6. However, if such is the case then you must remember to refactor the trunc() function in the code above to not use the arrow function expression (as it too was introduced in ES6).

You can also implement this using only the Math.floor() method like so:

function trunc(num) {
    const unsignedNum = Math.floor(Math.abs(num));
    return (num < 0) ? -unsignedNum : unsignedNum;
}

console.log(trunc(1234.567)); // 1234
console.log(trunc(-1234.567)); // -1234

console.log(trunc(1234)); // 1234
console.log(trunc(-1234)); // -1234

console.log(trunc(Infinity)); // Infinity
console.log(trunc(-Infinity)); // -Infinity

console.log(trunc(100000000000000000000000000000000.2345)); // 1e+32
console.log(trunc(-100000000000000000000000000000000.2345)); // -1e+32

Similarly, you can implement this using only the ceil() function like so:

function trunc(num) {
    const unsignedNum = Math.ceil(-Math.abs(num));
    return (num < 0) ? unsignedNum : -unsignedNum;
}

console.log(trunc(1234.567)); // 1234
console.log(trunc(-1234.567)); // -1234

console.log(trunc(1234)); // 1234
console.log(trunc(-1234)); // -1234

console.log(trunc(Infinity)); // Infinity
console.log(trunc(-Infinity)); // -Infinity

console.log(trunc(100000000000000000000000000000000.2345)); // 1e+32
console.log(trunc(-100000000000000000000000000000000.2345)); // -1e+32

Methods to Avoid

You may want to avoid the following methods for getting the integer part of a decimal number as they yield unexpected or wrong results in some cases:

parseInt():

Using parseInt() for getting the integer part of a decimal number would yield unexpected results in the following cases:

parseInt(Infinity, 10); // NaN
parseInt(-Infinity, 10); // NaN

parseInt(100000000000000000000000000000000.2345, 10); // 1
parseInt(-100000000000000000000000000000000.2345, 10); // 1

Bitwise Operators:

Using bitwise operators for getting the integer part of a decimal number would yield unexpected results in the following cases:

// inverting bits twice using bitwise NOT operator
~~Infinity; // 0
~~-Infinity; // 0

~~100000000000000000000000000000000.2345; // 0
~~-100000000000000000000000000000000.2345; // 0
// signed right shift bitwise operator
Infinity >> 0; // 0
-Infinity >> 0; // 0

100000000000000000000000000000000.2345 >> 0; // 0
-100000000000000000000000000000000.2345 >> 0; // 0
// OR bitwise operator
Infinity | 0; // 0
-Infinity | 0; // 0

100000000000000000000000000000000.2345 | 0; // 0
-100000000000000000000000000000000.2345 | 0; // 0

This post was published (and was last revised ) by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.