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
🌐
W3Schools
w3schools.com › js › js_number_methods.asp
JavaScript Number Methods
The methods above are not number methods. They are global JavaScript methods. The Number() method can be used to convert JavaScript variables to numbers:
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › parseInt
parseInt() - JavaScript | MDN
The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number
Number - JavaScript | MDN
There is no separate integer type in common everyday use. (JavaScript also has a BigInt type, but it's not designed to replace Number for everyday uses. 37 is still a number, not a BigInt.) When used as a function, Number(value) converts a string or other value to the Number type.
🌐
W3Schools
w3schools.com › jsref › jsref_parseint.asp
JavaScript parseInt() Method
The parseInt method parses a value as a string and returns the first integer. A radix parameter specifies the number system to use: 2 = binary, 8 = octal, 10 = decimal, 16 = hexadecimal. If radix is omitted, JavaScript assumes radix 10. If the value begins with "0x", JavaScript assumes radix 16. If the first character cannot be converted, NaN is returned.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number › parseInt
Number.parseInt() - JavaScript | MDN
The Number.parseInt() static method parses a string argument and returns an integer of the specified radix or base. function roughScale(x, base) { const parsed = Number.parseInt(x, base); if (Number.isNaN(parsed)) { return 0; } return parsed * 100; } console.log(roughScale(" 0xF", 16)); // ...
🌐
OpenReplay
blog.openreplay.com › convert-string-integer-javascript
How to Convert a String to an Integer in JavaScript
The parseInt() function parses a string and returns an integer. It also allows specifying the number system (radix) for conversion. const str = ""42""; const number = parseInt(str, 10); console.log(number); // Output: 42 · The second argument, ...
🌐
Vultr Docs
docs.vultr.com › javascript › global › parseInt
JavaScript parseInt() - Parse String to Integer | Vultr Docs
September 30, 2024 - It discards non-numeric characters occurring after the numbers. Recognize that spaces in strings can affect parsing results. Invoke parseInt() on a string with leading, embedded, or trailing spaces. ... With the input " 789 ", leading and trailing spaces are ignored, allowing parseInt() to correctly return the integer 789. Learn how the radix (base) parameter influences the parsing process. Parse a hexadecimal string using a radix of 16. ... This snippet converts the hexadecimal number "1A3" to its decimal equivalent, 419, using a radix of 16.
Find elsewhere
Top answer
1 of 16
3114

The simplest way would be to use the native Number function:

var x = Number("1000")

If that doesn't work for you, then there are the parseInt, unary plus, parseFloat with floor, and Math.round methods.

parseInt()

var x = parseInt("1000", 10); // You want to use radix 10
    // So you get a decimal number even with a leading 0 and an old browser ([IE8, Firefox 20, Chrome 22 and older][1])

Unary plus

If your string is already in the form of an integer:

var x = +"1000";

floor()

If your string is or might be a float and you want an integer:

var x = Math.floor("1000.01"); // floor() automatically converts string to number

Or, if you're going to be using Math.floor several times:

var floor = Math.floor;
var x = floor("1000.01");

parseFloat()

If you're the type who forgets to put the radix in when you call parseInt, you can use parseFloat and round it however you like. Here I use floor.

var floor = Math.floor;
var x = floor(parseFloat("1000.01"));

round()

Interestingly, Math.round (like Math.floor) will do a string to number conversion, so if you want the number rounded (or if you have an integer in the string), this is a great way, maybe my favorite:

var round = Math.round;
var x = round("1000"); // Equivalent to round("1000", 0)
2 of 16
306

Try parseInt function:

var number = parseInt("10");

But there is a problem. If you try to convert "010" using parseInt function, it detects as octal number, and will return number 8. So, you need to specify a radix (from 2 to 36). In this case base 10.

parseInt(string, radix)

Example:

var result = parseInt("010", 10) == 10; // Returns true

var result = parseInt("010") == 10; // Returns false

Note that parseInt ignores bad data after parsing anything valid.
This guid will parse as 51:

var result = parseInt('51e3daf6-b521-446a-9f5b-a1bb4d8bac36', 10) == 51; // Returns true
🌐
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.
🌐
Educative
educative.io › answers › how-to-convert-string-to-int-or-number-in-javascript
How to convert string to int or number in JavaScript
Use Number() or the unary + operator if you want to handle both integers and floats automatically. These methods are useful when you need to convert directly to a number regardless of the format of the input string.
🌐
Soledadpenades
soledadpenades.com › posts › 2024 › quick-conversion-to-integer-in-javascript
Quick conversion to integer in JavaScript | soledad penadés
I am not fully sure of what it does internally, but I'd adventure that it's something like this: the JS engine casts the string into a Number (floating point) then because you want to use the bit shift operator the engine turns the number into an integer, which might truncate any floating point ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › convert-a-string-to-an-integer-in-javascript
Convert a String to an Integer in JavaScript - GeeksforGeeks
July 11, 2025 - The number() method converts a string into an integer number. It works similarly to the unary plus operator.
🌐
Go Make Things
gomakethings.com › converting-strings-to-numbers-with-vanilla-javascript
Converting strings to numbers with vanilla JavaScript | Go Make Things
It accepts two arguments. The first argument is the string to convert. The second argument is called the radix. This is the base number used in mathematical systems. For our use, it should always be 10. var text = '42px'; var integer = parseInt(text, 10); // returns 42
🌐
freeCodeCamp
freecodecamp.org › news › how-to-convert-a-string-to-a-number-in-javascript
How to Convert a String to a Number in JavaScript
May 2, 2022 - It is important to make sure that your number is between the values of a signed 32 bit integer. const quantity = "2700000000"; console.log(~~quantity); To learn more about the bitwise NOT operator (~) , please read up in the documentation. Another way to convert a string to a number is to use the Math.floor() function.
🌐
Bacancy Technology
bacancytechnology.com › qanda › javascript › convert-string-to-integer-in-javascript
How to Convert String to Integer in JavaScript
August 5, 2025 - In JavaScript, you can convert a string to an integer using the following methods: let str = "123"; let num = parseInt(str); console.log(num); // 123 · let str = "123"; let num = +str; console.log(num); // 123 · let str = "123"; let num = Number(str); console.log(num); // 123 ·
🌐
Flexiple
flexiple.com › javascript › string-to-number
How to Convert a String to a Number In JavaScript - Flexiple
To convert a string to a number in JavaScript, the parseInt() function is utilized. This function parses a string argument and returns an integer of the specified radix or base. For instance, parseInt("123") returns the number 123.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › convert-a-string-to-number-in-javascript
Convert a String to Number in JavaScript - GeeksforGeeks
July 23, 2025 - The parseInt() method can be used to convert a numeric string into a number by passing it as an arguement to it. It will always return an integer either you pass a floating point string or integer string.
🌐
Built In
builtin.com › articles › javascript-string-to-a-number
How to Convert a JavaScript String to Number | Built In
Below are several different methods you can use to convert a string into a number in JavaScript with example code. parseInt() parses a string and returns a whole number. Spaces are allowed. Only the first number is returned. This method has a limitation though. If you parse the decimal number, it will be rounded off to the nearest integer ...
🌐
Reddit
reddit.com › r/htmx › how do you convert a number from an input to an int
r/htmx on Reddit: How do you convert a number from an input to an int
January 14, 2025 -

The API I'm interacting with accepts only integer. However my <input type="number"> sends the number as a string so the backend returns an "invalid type" error. I tried something like this:

document.body.addEventListener("htmx:configRequest", function (event) {
    event.detail.parameters["value"] = parseInt(event.detail.parameters["value"], 10);
});

But it's still being sent as a string. Did somebody have the same use-case and solved it?

Edit: If you stumbled upon this thread from a web search, please know that it's currently not possible to convert the value from a string to an integer. I need to send a JSON like this:

{
    "value": 100
}

But with the json-enc-custom extension I can only send it like this and it seems there's no easy way to serialize it into another type:

{
    "value": "100"
}

Toxicity and unwillingness to help in this thread made me abandon htmx.