How do I convert a float number to a whole number in JavaScript? How do I convert a float number to a whole number in JavaScript? javascript javascript

How do I convert a float number to a whole number in JavaScript?


var intvalue = Math.floor( floatvalue );var intvalue = Math.ceil( floatvalue ); var intvalue = Math.round( floatvalue );// `Math.trunc` was added in ECMAScript 6var intvalue = Math.trunc( floatvalue );

Math object reference


Examples

Positive
// value=x        //  x=5          5<x<5.5      5.5<=x<6  Math.floor(value) //  5            5            5Math.ceil(value)  //  5            6            6Math.round(value) //  5            5            6Math.trunc(value) //  5            5            5parseInt(value)   //  5            5            5~~value           //  5            5            5value | 0         //  5            5            5value >> 0        //  5            5            5value >>> 0       //  5            5            5value - value % 1 //  5            5            5
Negative
// value=x        // x=-5         -5>x>=-5.5   -5.5>x>-6Math.floor(value) // -5           -6           -6Math.ceil(value)  // -5           -5           -5Math.round(value) // -5           -5           -6Math.trunc(value) // -5           -5           -5parseInt(value)   // -5           -5           -5value | 0         // -5           -5           -5~~value           // -5           -5           -5value >> 0        // -5           -5           -5value >>> 0       // 4294967291   4294967291   4294967291value - value % 1 // -5           -5           -5
Positive - Larger numbers
// x = Number.MAX_SAFE_INTEGER/10 // =900719925474099.1// value=x            x=900719925474099    x=900719925474099.4  x=900719925474099.5           Math.floor(value) //  900719925474099      900719925474099      900719925474099Math.ceil(value)  //  900719925474099      900719925474100      900719925474100Math.round(value) //  900719925474099      900719925474099      900719925474100Math.trunc(value) //  900719925474099      900719925474099      900719925474099parseInt(value)   //  900719925474099      900719925474099      900719925474099value | 0         //  858993459            858993459            858993459~~value           //  858993459            858993459            858993459value >> 0        //  858993459            858993459            858993459value >>> 0       //  858993459            858993459            858993459value - value % 1 //  900719925474099      900719925474099      900719925474099
Negative - Larger numbers
// x = Number.MAX_SAFE_INTEGER/10 * -1 // -900719925474099.1// value = x      // x=-900719925474099   x=-900719925474099.5 x=-900719925474099.6Math.floor(value) // -900719925474099     -900719925474100     -900719925474100Math.ceil(value)  // -900719925474099     -900719925474099     -900719925474099Math.round(value) // -900719925474099     -900719925474099     -900719925474100Math.trunc(value) // -900719925474099     -900719925474099     -900719925474099parseInt(value)   // -900719925474099     -900719925474099     -900719925474099value | 0         // -858993459           -858993459           -858993459~~value           // -858993459           -858993459           -858993459value >> 0        // -858993459           -858993459           -858993459value >>> 0       //  3435973837           3435973837           3435973837value - value % 1 // -900719925474099     -900719925474099     -900719925474099


Bitwise OR operator

A bitwise or operator can be used to truncate floating point figures and it works for positives as well as negatives:

function float2int (value) {    return value | 0;}

Results

float2int(3.1) == 3float2int(-3.1) == -3float2int(3.9) == 3float2int(-3.9) == -3

Performance comparison?

I've created a JSPerf test that compares performance between:

  • Math.floor(val)
  • val | 0 bitwise OR
  • ~~val bitwise NOT
  • parseInt(val)

that only works with positive numbers. In this case you're safe to use bitwise operations well as Math.floor function.

But if you need your code to work with positives as well as negatives, then a bitwise operation is the fastest (OR being the preferred one). This other JSPerf test compares the same where it's pretty obvious that because of the additional sign checking Math is now the slowest of the four.

Note

As stated in comments, BITWISE operators operate on signed 32bit integers, therefore large numbers will be converted, example:

1234567890  | 0 => 123456789012345678901 | 0 => -539222987


Note: You cannot use Math.floor() as a replacement for truncate, because Math.floor(-3.1) = -4 and not -3 !!

A correct replacement for truncate would be:

function truncate(value){    if (value < 0) {        return Math.ceil(value);    }    return Math.floor(value);}