Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'd like to convert a float to a whole number in JavaScript. Actually, I'd like to know how to do BOTH of the standard conversions: by truncating and by rounding. And efficiently, not via converting to a string and parsing.

like image 376
mcherm Avatar asked Feb 27 '09 20:02

mcherm


People also ask

How do you convert a float to a whole number?

Method 1: Conversion using int(): To convert a float value to int we make use of the built-in int() function, this function trims the values after the decimal point and returns only the integer/whole number part.

How do you get a whole number in JavaScript?

Use the Math. round() function to round the result to the nearest integer.

How do you turn a number into a whole number?

To round a decimal to the nearest whole number analyse the digit at the first decimal place i.e., tenths place. If the tenths place value is 5 or greater than 5, then the digit at the ones place increases by 1 and the digits at the tenths place and thereafter becomes 0.


2 Answers

var intvalue = Math.floor( floatvalue ); var intvalue = Math.ceil( floatvalue );  var intvalue = Math.round( floatvalue );  // `Math.trunc` was added in ECMAScript 6 var 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            5 Math.ceil(value)  //  5            6            6 Math.round(value) //  5            5            6 Math.trunc(value) //  5            5            5 parseInt(value)   //  5            5            5 ~~value           //  5            5            5 value | 0         //  5            5            5 value >> 0        //  5            5            5 value >>> 0       //  5            5            5 value - value % 1 //  5            5            5 
Negative
// value=x        // x=-5         -5>x>=-5.5   -5.5>x>-6  Math.floor(value) // -5           -6           -6 Math.ceil(value)  // -5           -5           -5 Math.round(value) // -5           -5           -6 Math.trunc(value) // -5           -5           -5 parseInt(value)   // -5           -5           -5 value | 0         // -5           -5           -5 ~~value           // -5           -5           -5 value >> 0        // -5           -5           -5 value >>> 0       // 4294967291   4294967291   4294967291 value - 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      900719925474099 Math.ceil(value)  //  900719925474099      900719925474100      900719925474100 Math.round(value) //  900719925474099      900719925474099      900719925474100 Math.trunc(value) //  900719925474099      900719925474099      900719925474099 parseInt(value)   //  900719925474099      900719925474099      900719925474099 value | 0         //  858993459            858993459            858993459 ~~value           //  858993459            858993459            858993459 value >> 0        //  858993459            858993459            858993459 value >>> 0       //  858993459            858993459            858993459 value - 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.6  Math.floor(value) // -900719925474099     -900719925474100     -900719925474100 Math.ceil(value)  // -900719925474099     -900719925474099     -900719925474099 Math.round(value) // -900719925474099     -900719925474099     -900719925474100 Math.trunc(value) // -900719925474099     -900719925474099     -900719925474099 parseInt(value)   // -900719925474099     -900719925474099     -900719925474099 value | 0         // -858993459           -858993459           -858993459 ~~value           // -858993459           -858993459           -858993459 value >> 0        // -858993459           -858993459           -858993459 value >>> 0       //  3435973837           3435973837           3435973837 value - value % 1 // -900719925474099     -900719925474099     -900719925474099 
like image 131
moonshadow Avatar answered Sep 25 '22 19:09

moonshadow


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) == 3 float2int(-3.1) == -3 float2int(3.9) == 3 float2int(-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 => 1234567890 12345678901 | 0 => -539222987 
like image 43
Robert Koritnik Avatar answered Sep 23 '22 19:09

Robert Koritnik