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.


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 = 5.5

Math.floor(value) //  5
Math.ceil(value)  //  6
Math.round(value) //  6
Math.trunc(value) //  5
parseInt(value)   //  5
~~value           //  5
value | 0         //  5
value >> 0        //  5
value >>> 0       //  5
value - value % 1 //  5
Negative
value = -5.5

Math.floor(value) // -6
Math.ceil(value)  // -5
Math.round(value) // -5
Math.trunc(value) // -5
parseInt(value)   // -5
value | 0         // -5
~~value           // -5
value >> 0        // -5
value >>> 0       // 4294967291
value - value % 1 // -5
Positive - Larger numbers
value = Number.MAX_SAFE_INTEGER/10 // 900719925474099.1

Math.floor(value) //  900719925474099
Math.ceil(value)  //  900719925474100
Math.round(value) //  900719925474099
Math.trunc(value) //  900719925474099
parseInt(value)   //  900719925474099
value | 0         //  858993459
~~value           //  858993459
value >> 0        //  858993459
value >>> 0       //  858993459
value - value % 1 //  900719925474099
Negative - Larger numbers
value = Number.MAX_SAFE_INTEGER/10 * -1 // -900719925474099.1

Math.floor(value) // -900719925474100
Math.ceil(value)  // -900719925474099
Math.round(value) // -900719925474099
Math.trunc(value) // -900719925474099
parseInt(value)   // -900719925474099
value | 0         // -858993459
~~value           // -858993459
value >> 0        // -858993459
value >>> 0       //  3435973837
value - value % 1 // -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) == 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 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
    

    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);
    }
    
    链接地址: http://www.djcxy.com/p/66838.html

    上一篇: Google App Inventor对于移动软件开发意味着什么?

    下一篇: 如何在JavaScript中将浮点数转换为整数?