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
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
Discussions

[AskJS] Its about time javascript should get a "integer" and "float" data type.
JavaScript numbers are double-precision floating-point values, also known as a double in other languages like C. This covers the "float" side of things. Now, a double-precision floating-point value can perfectly represent integers up to 52 bits, which is a lot bigger than the 32 bits an int can typically store in other languages. JavaScript bitwise operators such as & or | or ^ also convert their values to 32-bit integers before doing their work. So, just use regular JavaScript numbers to perform your integer arithmetic, and then throw in a few | 0 operations if you want the rounding / clamping behavior found in other language's 32-bit int types. In other words, everything you want is already present. More on reddit.com
🌐 r/javascript
46
0
August 10, 2022
Hey JavaScript, can you turn my float into int properly?
For those who haven’t seen this, it’s because parseInt() takes a string argument. 0.0000005 automatically typecasts to the string ‘5e-7’ in scientific notation. parseInt sees the 5, uses it, sees the e which is not a decimal number, and stops. If you do parseInt(0.0000005, 16) to parse the string in base 16, the answer becomes 94 instead of 5 because e is also a hex digit. More on reddit.com
🌐 r/ProgrammerAnimemes
86
1701
February 3, 2022
Is there any reason to have integer division?
Integer division is generally a native operation on modern CPUs. To the extent that a programming language is meant to allow programmers to tell a computer what to do, this seems self-defeating. Casting to floats then dividing then casting back may be less efficient than integer divsion. The two operations are not numerically equivalent as information is lost when casting a N-bit int to an N-bit float as the significand will necessarily be smaller than the int to make room for the exponent and sign bit fields. More on reddit.com
🌐 r/ProgrammingLanguages
96
21
August 6, 2024
Is it ok to do "if (x == 1)" or "if (x == 0)" when x is a float?
Yes, with caveats: The values 0 and 1 are exactly representable as IEEE-754 floats, which nearly all modern implementations of floats use. It is legal C code to write if (x == 1) or if (x == 0) where x is a float. Float arithmetic generally introduces round-off error. The magnitude of the error depends on the operation, but it is typically small, in the 6th or 7th significant figure for floats. To account for round-off error, it is standard practice to compare with a tolerance like if (fabsf(x - 1.0f) < 1e-6f). See also What Every Programmer Should Know About Floating-Point Arithmetic . More on reddit.com
🌐 r/C_Programming
64
61
December 24, 2023
🌐
W3Schools
w3schools.com › js › js_number_methods.asp
JavaScript Number Methods
Safe integers are all integers from -(253 - 1) to +(253 - 1). This is safe: 9007199254740991.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › trunc
Math.trunc() - JavaScript - MDN Web Docs
July 10, 2025 - The Math.trunc() static method returns the integer part of a number by removing any fractional digits.
🌐
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.
🌐
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 - We can apply the bitwise right shift operator (represented by >> characters). It will convert the float value to an integer. Underneath at the binary level, the bitwise right SHIFT operator shifts the binary bits of the operand to the right ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-convert-a-float-number-to-the-whole-number-in-javascript
How to Convert a Float Number to the Whole Number in JavaScript? - GeeksforGeeks
July 11, 2025 - ... // float value is 4.59; let x = 4.59; let z = Math.round(x); console.log("Converted value of " + x + " is " + z); ... Return the integer part of a floating-point number by removing the fractional digits.
Find elsewhere
🌐
Reddit
reddit.com › r/javascript › [askjs] its about time javascript should get a "integer" and "float" data type.
r/javascript on Reddit: [AskJS] Its about time javascript should get a "integer" and "float" data type.
August 10, 2022 -

After all the improvements/features and fixes which were made to javascript (or precisely ECMAScript) since ES6 (ES2015) till now (ES2022), isn't it about time to fix the ultimate pending weakness i.e. having an Integer and a Float type as well (just like "var" keyword was/is there but they added "let" and "const" as a better semantics and good practice) and not just mashup everything in Number.

Heck, we even got a big int to represent big integers (no floats allowed) but we still don't have Integer's and Floats which are SUPER useful in almost every scenario.

So, why is it still not added and not planned as well? Those are VERY important data types and MOST languages HAVE those data types as they are NEEDED, why is it not planned for ECMAScript? Is it planned? Do you want to see this added?

🌐
Rip Tutorial
riptutorial.com › float to integer
JavaScript Tutorial => Float to Integer
The floor function returns the first integer less than or equal to the float.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number › parseFloat
Number.parseFloat() - JavaScript - MDN - Mozilla
The Number.parseFloat() static method parses an argument and returns a floating point number. If a number cannot be parsed from the argument, it returns NaN. function circumference(r) { if (Number.isNaN(Number.parseFloat(r))) { return 0; } return parseFloat(r) * 2.0 * Math.PI; } console.lo...
🌐
Vultr Docs
docs.vultr.com › javascript › examples › check-if-a-number-is-float-or-integer
JavaScript Program to Check if a Number is Float or Integer | Vultr Docs
November 7, 2024 - Number.isInteger() directly checks if the provided value is an integer, offering an efficient and ready-to-use solution without additional coding. Determining the type of a numerical value as float or integer in JavaScript can be achieved through various methods, each suited for different situations.
🌐
Byby
byby.dev › js-float-to-int
How to convert float to integer in JavaScript
You can use various methods that either round, truncate, or parse the float value. Depending on your specific use case, you can choose the most appropriate method: Using Math.trunc(). This method returns the integer part of a number by removing any fractional digits. In other words, it truncates (cuts off) the dot and the digits to the right of it, no matter whether the argument is a positive or negative number.
🌐
Javascriptf1
javascriptf1.com › snippet › convert-a-float-number-to-integer-in-javascript
Convert a float number to integer in JavaScript - JavaScriptF1.com
November 2, 2021 - If you want the integer (whole) part of a float without rounding, you can use the Math.trunc function. The Math.trunc() function returns the integer part of a number by removing any fractional digits.
🌐
Codemia
codemia.io › knowledge-hub › path › how_do_i_convert_a_float_number_to_a_whole_number_in_javascript
How do I convert a float number to a whole ...
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-convert-a-float-number-to-a-whole-number-in-javascript.php
How to Convert a Float Number to a Whole Number in JavaScript
You can use the trunc() method to get the integer part of a number by removing any fractional digits. This method was added in ECMAScript 6 and supported by the latest browsers such as Chrome, Firefox, Safari, Opera, and Edge browsers.
🌐
W3docs
w3docs.com › javascript
How to Convert a Float Number to Whole Number in JavaScript
The parseInt() function parses the string argument and returns an integer of the given radix. ... The ~~ operator rounds a number towards 0 if the operand is a number and it’s not NaN or Infinity. ... The OR operator rounds a number towards 0. ... There are two types of numbers in JavaScript: Regular and BigInt. Regular is also known as “double-precision floating-point numbers”. The math is a built-in object which has properties and methods for mathematical constants and functions.
🌐
Reddit
reddit.com › r/programmeranimemes › hey javascript, can you turn my float into int properly?
[Mature Content] r/ProgrammerAnimemes on Reddit: Hey JavaScript, can you turn my float into int properly?
February 3, 2022 - I haven’t actually tried that (I’m on my phone) but casting to a number and then making that an integer seems the most straightforward. Plus it’s not limited to 31 bits (try 3_000_000_000 in yours) ... Javascript dont has int or float type, all they has is number type and bignumber, so ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-program-to-check-if-a-number-is-float-or-integer
JavaScript Program to Check if a Number is Float or Integer - GeeksforGeeks
July 23, 2025 - The Modulus Operator calculates the remainder when the number is divided by 1. If the remainder is 0, the number is an integer; otherwise, it's a float. ... Example: This example uses the Modulus Operator (%) to check the Number.
🌐
TutorialsPoint
tutorialspoint.com › 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?
July 20, 2022 - We have discussed the four approaches to converting the float number to a whole number. The second approach is the easiest one as it parses the integer only from the float number.