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
Answer from moonshadow on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › round
Math.round() - JavaScript | MDN
If the fractional portion of the argument is greater than 0.5, the argument is rounded to the integer with the next higher absolute value. If it is less than 0.5, the argument is rounded to the integer with the lower absolute value.
🌐
W3Schools
w3schools.com › jsref › jsref_round.asp
JavaScript Math round() Method
The Math.round() method rounds a number to the nearest integer. 2.49 will be rounded down (2), and 2.5 will be rounded up (3). The Math.abs() Method The Math.ceil() Method The Math.floor() Method The Math.round() Method The Math.fround() Method ...
Top answer
1 of 16
2261
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
2 of 16
348

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
🌐
pawelgrzybek
pawelgrzybek.com › rounding-and-truncating-numbers-in-javascript
Rounding and truncating numbers in JavaScript | pawelgrzybek.com
Luckily there is a way to use this without ES6 support (thanks to Johny who suggested this solution in comments below). We can use bitwise operators to accomplish this task. Unfortunately there are some restriction as well. All bitwise operations work on signed 32-bit integers. Using them converts a float to an integer.
🌐
Stack Abuse
stackabuse.com › how-to-round-doubles-or-floats-in-javascript
How to Round Doubles/Floats in JavaScript
February 22, 2022 - In this tutorial - learn how to round decimal numbers (floats/doubles) in JavaScript to whole integers - up, down or nearest.
🌐
Code Highlights
code-hl.com › home › javascript › tutorials
5 Easy Ways to Convert JavaScript Float to Int | Code Highlights
June 4, 2024 - In this tutorial, we explored five simple methods to convert a JavaScript float to an int: Math.floor(): Rounds down to the nearest integer.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › data types
Numbers
Also in many countries, the currency symbol goes after the amount, so we have "19€" and would like to extract a numeric value out of that. That’s what parseInt and parseFloat are for. They “read” a number from a string until they can’t. In case of an error, the gathered number is returned. The function parseInt returns an integer, whilst parseFloat will return a floating-point number:
🌐
TutorialsPoint
tutorialspoint.com › How-can-I-round-down-a-number-in-JavaScript
How can I round down a number in JavaScript?
In the above output, users can see that after rounding down, 10234.2433341 becomes 10234, which is the nearest small integer to 10234.2433341. If we perform the Bitwise operator on any float number, it removes the decimal part of the number.
Find elsewhere
🌐
Byby
byby.dev › js-float-to-int
How to convert float to integer in JavaScript
Using Math.floor(): This function rounds down to the nearest integer. When you apply it to a positive float, it will return the largest integer less than or equal to the float.
🌐
Linux Hint
linuxhint.com › different-methods-to-round-float-numbers-in-javascript
Different Methods to Round Float Numbers in JavaScript?
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript float to int
How to Convert a Float Number to Int in JavaScript | Delft Stack
February 2, 2024 - Else, if the value after the decimal is lesser than 5, it is rounded to the lower integer. Hence, based on the value post the decimal point, Math.round() function can behave similar to Math.floor() or Math.ceil() functions.
🌐
W3Schools
w3schools.com › jsref › jsref_floor.asp
JavaScript Math floor() Method
The Math.floor() method rounds a number DOWN to the nearest integer. The Math.abs() Method The Math.ceil() Method The Math.floor() Method The Math.round() Method The Math.fround() Method The Math.f16round() Method The Math.trunc() Method ...
🌐
SheCodes
shecodes.io › athena › 12443-how-to-round-up-a-float-in-javascript
[JavaScript] - How to round up a float in JavaScript - | SheCodes
Learn how to use the Math.ceil() function to round up a float in JavaScript and log it to the console.
🌐
Kodeclik
kodeclik.com › javascript-round-down
How to round down a number in Javascript
October 24, 2025 - Note that Math.floor() rounds down to the nearest integer, regardless of whether the input is an integer or a floating-point number.
🌐
Sonasa
sonasa.cv › c5tan › javascript-float-to-int-round-down
javascript float to int round down
Math.round () is a built-in method in JavaScript that cuts off the floating-point value and returns an integer. (A1) Multiply the given number by an offset first. The only difference is with negative numbers, where floor() will round -4.5 down to -5 whereas typecasting would return -4.
🌐
CoreUI
coreui.io › answers › how-to-round-a-number-in-javascript
How to round a number in JavaScript · CoreUI
September 26, 2025 - Use Math.round() to round numbers to the nearest integer or combine with multiplication for decimal precision in JavaScript.