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?
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? |
|||
|
parseInt or unary plus or even parseFloat with floor or Math.round parseInt:
unary plus if your string is already in the form of an integer:
if your string is or might be a float and you want an integer:
or, if you're going to be using Math.floor several times:
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.
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:
You don't see this much. valueOf is used mostly internally, according to w3c
|
|||||||||||||||||||||
|
Try parseInt function:
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.
Example:
|
|||
|
There are two main ways to convert a string to a number in javascript. One way is to parse it and the other way is to change its type to a Number. All of the tricks in the other answers (e.g. unary plus) involve implicitly coercing the type of the string to a number. You can also do the same thing explicitly with the Number function. Parsing
parseInt and parseFloat are the two functions used for parsing strings to numbers. Parsing will stop silently if it hits a character it doesn't recognise, which can be useful for parsing strings like "92px", but it's also somewhat dangerous, since it won't give you any kind of error on bad input, instead you'll get back NaN unless the string starts with a number. Whitespace at the beginning of the string is ignored. Here's an example of it doing something different to what you want, and giving no indication that anything went wrong:
It's good practice to always specify the radix as the second argument. In older browsers, if the string started with a 0, it would be interpreted as octal if the radix wasn't specified which took a lot of people by surprise. The behaviour for hexadecimal is triggered by having the string start with 0x if no radix is specified, e.g. Changing the Type of a String to a Number All of the other tricks mentioned above that don't use parseInt, involve implicitly coercing the string into a number. I prefer to do this explicitly,
This has different behavior to the parse methods (although it still ignores whitespace). It's more strict: if it doesn't understand the whole of the string than it returns Obviously, converting to a Number gives you a value that might be a float rather than an integer, so if you want an integer, you need to modify it. There are a few ways of doing this:
Any bitwise operator (here I've done a bitwise or, but you could also do double negation as in an earlier answer or a bitshift) will convert the value to a 32bit integer, and most of them will convert to a signed integer. Note that this will not do want you want for large integers. If the integer cannot be represented in 32bits, it will wrap.
To work correctly with larger numbers, you should use the rounding methods
Bear in mind that all of these methods understand exponential notation, so Custom It's unlikely that either of these methods do exactly what you want. For example, usually I would want an error thrown if parsing fails, and I don't need support for Infinity, exponentials or leading whitespace. Depending on your usecase, sometimes it makes sense to write a custom conversion function. Always check that the output of Number or one of the parse methods is the sort of number you expect. You will almost certainly want to use |
|||||||||||||
|
ParseInt() and + are different
|
|||
|
Try parseInt.
|
|||||||||
|
I posted the wrong answer here, sorry. fixed. This is an old question, but I love this trick:
The double bitwise negative drops off anything after the decimal point AND converts it to a number format. I've been told it's slightly faster than calling functions and whatnot, but I'm not entirely convinced. EDIT: Another method I just saw here (a question about the javascript >>> operator, which is a zero-fill right shift) which shows that shifting a number by 0 with this operator converts the number to a uint32 which is nice if you also want it unsigned. Again, this converts to an unsigned integer, which can lead to strange behaviors if you use a signed number.
|
||||
|
Though and old question, but maybe this can be helpful to someone. I use this way of converting string to
So, when multiplying by 1, the value does not change, but js automatically returns a number. But as it is shown below, this should be used if you are sure that the you can create simple function to use, e.g.
|
|||||||||
|
Also as a side note: Mootools has the function toInt() which is used on any native string (or float (or integer)).
|
|||||||||||||
|
Beware if you use parseInt to convert a float in scientific notation! For example:
will result in
instead of
|
|||||
|
By using parsing method of JavaScript, you can convert string to integer:
|
||||
|
Thank you for your interest in this question.
Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?