javascript tutorial - conversion a float number to a whole number - javascript - java script - javascript array



Problem:

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

Solution 1:

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 );

click below button to copy the code. By JavaScript tutorial team

Solution 2:

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
click below button to copy the code. By JavaScript tutorial team

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
click below button to copy the code. By JavaScript tutorial team

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
click below button to copy the code. By JavaScript tutorial team

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
click below button to copy the code. By JavaScript tutorial team

Solution 3:

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;
}
click below button to copy the code. By JavaScript tutorial team

Results

float2int(3.1) == 3
float2int(-3.1) == -3
float2int(3.9) == 3
float2int(-3.9) == -3
click below button to copy the code. By JavaScript tutorial team

Solution 4:

A correct replacement for truncate would be:

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

    return Math.floor(value);
}
click below button to copy the code. By JavaScript tutorial team

Solution 5:

A double bitwise not operator can be used to truncate floats. The other operations Math.floor, Math.ceil , and Math.round

> ~~2.5
2
> ~~(-1.4)
-1
click below button to copy the code. By JavaScript tutorial team

Related Searches to javascript tutorial - conversion a float number to a whole number