views:

1365

answers:

5

How do I convert a string into an integer in JavaScript?

Is it possible to do this automatically, or do I have to write a subroutine to do it manually?

+5  A: 

Try parseInt.

var number = parseInt("10", 10); //number will have value of 10.
SolutionYogi
You probably want to include the _radix_ with that, too: _var number = parseInt("10", 10);_
Joel Coehoorn
In fact, since you have the accepted answer let me fix that for you...
Joel Coehoorn
+3  A: 

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
Zanoni
+7  A: 

parseInt or unary plus or even parseFloat with floor or Math.round

parseInt:

var x = parseInt("1000",10); // you want to use radix
    // of 10 so you get a decimal number even with a leading 0

unary plus if your string is already in the form of an integer:

var x = +"1000";

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

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

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)

You don't see this much. valueOf is used mostly internally, according to w3c

var x = "1000".valueOf();
Nosredna
For the last case, if you just want to truncate the number (round toward zero) you can still use parseInt. It will parse the number up to the period.
Matthew Crumley
Yes. Any time I'm using "floor" much I do var floor=Math.floor which tightens things up a bit. I just wanted to show what happens with unary plus when you have a float. People run into trouble all the time because 1) People end up with strings when they want numbers. 2) JavaScript uses "+" for both addition and string concatenation.
Nosredna
Updated answer. Thanks Matthew.
Nosredna
Added awesome "Math.round" solution!
Nosredna
Added valueOf() solution.
Nosredna
+1  A: 

Also as a side note: Mootools has the function toInt() which is used on any native string (or float (or integer)).

"2".toInt()   // 2
"2px".toInt() // 2
2.toInt()     // 2
henrikh
Cool! I didn't know that. MooTools is perennially on my list of "to learn."
Nosredna
The third example causes a `SyntaxError`, you should use a double dot, e.g.: `2..toInt();` the first dot will end the representation of a `Number` literal and the second dot is the property accessor.
CMS
A: 

Beware if you use parseInt to convert a float in scientific notation! For example: parseInt("5.6e-14") will result in 5 instead of 0

tom