Coder Perfect

Is there a way to get rid of the decimal element of a JavaScript number?

Problem

I’ve got the results of a division and want to get rid of the decimal part of the resultant value.

I’m not sure how I’m going to accomplish it.

Asked by JacobTheDev

Solution #1

You could use…

…depending on how you wanted the decimal to be removed.

While Math.trunc() isn’t currently supported on all systems (most notably Internet Explorer), a polyfill could be used in the meantime.

The use of a bitwise operator (e.g. |0) is another approach of truncating the fractional portion with excellent platform support. When you use a bitwise operator on a number, the operand is treated as a signed 32bit integer, which removes the fractional component. Keep in mind that this will mutilate numbers that are greater than 32 bits.

You could also be referring to the inaccuracy of floating point arithmetic’s decimal rounding.

What Every Computer Scientist Should Know About Floating-Point Arithmetic is required reading.

Answered by alex

Solution #2

To truncate the decimal, you can use bitwise operations.

e.g.

var x = 9 / 2;
console.log(x); // 4.5

x = ~~x;
console.log(x); // 4

x = -3.7
console.log(~~x) // -3
console.log(x | 0) // -3
console.log(x << 0) // -3

Bitwise operations are significantly faster than Math functions. By a small margin, the double not bitwise operator appears to outperform the x | 0 and x 0 bitwise operations.

// 952 milliseconds
for (var i = 0; i < 1000000; i++) {
    (i * 0.5) | 0;
}

// 1150 milliseconds
for (var i = 0; i < 1000000; i++) {
    (i * 0.5) << 0;
}

// 1284 milliseconds
for (var i = 0; i < 1000000; i++) {
    Math.trunc(i * 0.5);
}

// 939 milliseconds
for (var i = 0; i < 1000000; i++) {
    ~~(i * 0.5);
}

It’s also worth mentioning that the bitwise not operator takes precedence over arithmetic operations, so you might need to use parentheses to get the desired result:

x = -3.7

console.log(~~x * 2) // -6
console.log(x * 2 | 0) // -7
console.log(x * 2 << 0) // -7

console.log(~~(x * 2)) // -7
console.log(x * 2 | 0) // -7
console.log(x * 2 << 0) // -7

More info about the double bitwise not operator can be found at Double bitwise NOT (~~)

Answered by Braden Steffaniak

Solution #3

You might also consider

parseInt(a/b)

Answered by Hari Pachuveetil

Solution #4

You can also use the following code to display a specific number of digits after the decimal point (here 2 digits):

Answered by Mahdi ghafoorian

Solution #5

For Example:

var x = 9.656;
x.toFixed(0);           // returns 10
x.toFixed(2);           // returns 9.66
x.toFixed(4);           // returns 9.6560
x.toFixed(6);           // returns 9.656000 

or

parseInt("10");         // returns 10
parseInt("10.33");      // returns 10
parseInt("10 20 30");   // returns 10
parseInt("10 years");   // returns 10
parseInt("years 10");   // returns NaN  

Answered by kelly k audrey

Post is based on https://stackoverflow.com/questions/7641818/how-can-i-remove-the-decimal-part-from-javascript-number