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
🌐
W3Schools
w3schools.com › js › js_numbers.asp
JavaScript Numbers
Unlike many other programming ... like integers, short, long, floating-point etc. JavaScript numbers are always stored as double precision floating point numbers, following the international IEEE 754 standard. This format stores numbers in 64 bits, where the number (the fraction) is stored in bits 0 to 51, the exponent ...
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
🌐
Rip Tutorial
riptutorial.com › integer to float
JavaScript Tutorial => Integer to Float
In JavaScript, all numbers are internally represented as floats. This means that simply using your integer as a float is all that must be done to convert it.
🌐
EyeHunts
tutorial.eyehunts.com › home › javascript int to float | conversion example code
JavaScript int to float | Conversion example code
July 4, 2022 - Use parseFloat() with to toFixed() method to convert int to float in JavaScript. Where toFixed() method formats a number using fixed-point notation. Read MDN Web Docs for full reference.
🌐
GeeksforGeeks
geeksforgeeks.org › 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
September 16, 2024 - Return the integer part of a floating-point number by removing the fractional digits. ... Example: Here we are using Math.trunc() method to remove the fractional digits.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number › parseFloat
Number.parseFloat() - JavaScript | MDN
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...
Find elsewhere
🌐
Reddit
reddit.com › r/learnjavascript › does javascript supports conversion of integer x to x.0 as typeof number?
r/learnjavascript on Reddit: Does JavaScript supports conversion of integer X to X.0 as typeof number?
January 1, 2023 -

Given string lets say “5” I want to convert it to 5.0 (typeof Number)

Already tried combinations/permutations of all the following: parseFloat() toFixed() toPrecision() Number() Also tried with “+” (implicit Number casting)

I always end up with either typeof string Or just the number 5

Its for an API that expects “float” but does not accept integers (5 is invalid, 5.0 is valid)

Any ideas would be appreciated.

🌐
W3Schools
w3schools.com › jsref › jsref_parsefloat.asp
JavaScript parseFloat() Method
HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial · HTML Reference CSS Reference JavaScript Reference SQL Reference Python Reference W3.CSS Reference Bootstrap Reference PHP Reference HTML Colors Java Reference AngularJS Reference jQuery Reference · HTML Examples CSS Examples JavaScript Examples How To Examples SQL Examples Python Examples W3.CSS Examples Bootstrap Examples PHP Examples Java Examples XML Examples jQuery Examples
🌐
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?
let result = number | 0; // number is the float number · In the given example, we are performing the Bitwise OR operation of 209.54321 with the 0 and converting it into the whole number. After that, we have rendered the output using JavaScript.
🌐
W3docs
w3docs.com › javascript
How to Convert a Float Number to Whole Number in JavaScript
There are various easy methods to convert float number to the whole number in JavaScript. Let’s discuss each of them and see examples. Here are several built-in functions used for rounding. The math.floor unction rounds the number passed as parameter to its nearest integer in downward direction:
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-string-into-float-in-javascript
How to convert string into float in JavaScript?
Here is an example of how to use the + operator ? <!DOCTYPE html> <html> <body> <p id="demo"></p> <script> let string = "3.4"; let float = +string; document.getElementById("demo").innerHTML = float; //output:3.4 </script> </body> </html> The parseInt() built-in JavaScript function converts a text to an integer or whole number.
🌐
CodingTechRoom
codingtechroom.com › question › -int-to-float-math-round-javascript
How to Convert an Integer to Float and Use Math.round in JavaScript? - CodingTechRoom
You wish to round up the resulting float to the nearest whole number. Use the parseFloat function to convert the integer to a float.
🌐
TutorialsTeacher
tutorialsteacher.com › javascript › javascript-number
JavaScript Numers: Integer, Float, Binary, Exponential, Hexadecimal, Octal
The Number is a primitive data ... integer, float, binary, octal, hexadecimal, and exponential values in JavaScript. The Number type in JavaScript is double-precision 64 bit binary format like double in C# and Java. It follows the international IEEE 754 standard. The first character in a number type must be an integer value, and it must not be enclosed in quotation marks. The following example shows the ...
🌐
Byby
byby.dev › js-float-to-int
How to convert float to integer in JavaScript
Only use bitwise operations when you are confident that the range of input falls within the range of 32-bit integers. You can use the bitwise OR operator (|) with zero to get the whole number part of a float. For example, 3.14 | 0 will return 3. This works because the OR operator sets each ...
🌐
Reasonml-old
reasonml-old.github.io › guide › language › integer-and-float
Integer & Float
Careful when you bind to JavaScript numbers! Long ones might be truncated. Bind JS number as float instead.
🌐
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 if you want to remove decimal point in your number, use the floor function
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › parseFloat
parseFloat() - JavaScript | MDN
The parseFloat() function parses a string argument and returns a floating point number. function circumference(r) { return parseFloat(r) * 2.0 * Math.PI; } console.log(circumference(4.567)); // Expected output: 28.695307297889173 console.log(circumference("4.567abcdefgh")); // Expected output: ...
🌐
W3Schools
w3schools.com › js › js_number_methods.asp
JavaScript Number Methods
HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial · HTML Reference CSS Reference JavaScript Reference SQL Reference Python Reference W3.CSS Reference Bootstrap Reference PHP Reference HTML Colors Java Reference AngularJS Reference jQuery Reference · HTML Examples CSS Examples JavaScript Examples How To Examples SQL Examples Python Examples W3.CSS Examples Bootstrap Examples PHP Examples Java Examples XML Examples jQuery Examples