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

Find out how to get the digits before a decimal point in JavaScript

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+
Math.trunc(1234.567); // output: 1234
Math.trunc(-1234.567); // output: -1234

Math.trunc(1234); // output: 1234
Math.trunc(-1234); // output: -1234

Math.trunc(Infinity); // output: Infinity
Math.trunc(-Infinity); // output: -Infinity

Math.trunc(100000000000000000000000000000000.2345); // output: 1e+32
Math.trunc(-100000000000000000000000000000000.2345); // output: -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);

trunc(1234.567); // output: 1234
trunc(-1234.567); // output: -1234

trunc(1234); // output: 1234
trunc(-1234); // output: -1234

trunc(Infinity); // output: Infinity
trunc(-Infinity); // output: -Infinity

trunc(100000000000000000000000000000000.2345); // output: 1e+32
trunc(-100000000000000000000000000000000.2345); // output: -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).

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); // output: NaN
parseInt(-Infinity, 10); // output: NaN

parseInt(100000000000000000000000000000000.2345, 10); // output: 1
parseInt(-100000000000000000000000000000000.2345, 10); // output: 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; // output: 0
~~-Infinity; // output: 0

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

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

100000000000000000000000000000000.2345 | 0; // output: 0
-100000000000000000000000000000000.2345 | 0; // output: 0

Hope you found this post useful. It was published . Please show your love and support by sharing this post.