Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript parseInt on large negative number gives NaN

Tags:

javascript

nan

When I do this:

var x = parseInt("–2147483648");
console.log(x);

I get the value as:

NaN

Why does this happen?

I want to test if a number is in the range of C (int), so I am doing the above, but it does not work. Also, I want to do this for C (long), is there a way to this?

For example: If I do:

var x = parseInt("-9223372036854775808");
console.log(x);

Now, I know that (-+)2^53 is the limit of numbers in Javascript. Is there some other way to test if the given value in a form is actually in the range of long or int?

like image 716
Rishi Avatar asked Jul 24 '13 05:07

Rishi


1 Answers

It should work fine, the problem is that you're using the wrong character, an ndash vs a hyphen -:

var x = parseInt("-2147483648");
console.log(x);

If you copy/paste that you'll see that it works now.

like image 191
elclanrs Avatar answered Sep 28 '22 07:09

elclanrs