大负数上的 Javascript parseInt 给出 NaN

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17825781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:46:09  来源:igfitidea点击:

Javascript parseInt on large negative number gives NaN

javascriptnan

提问by Rishi

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?

我想测试一个数字是否在C(int)的范围内,所以我在做上面的,但它不起作用。另外,我想为 C(长)做这个,有没有办法做到这一点?

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?

现在,我知道 (-+)2^53 是 Javascript 中的数字限制。是否有其他方法可以测试表单中的给定值是否实际上在 long 或 int 的范围内?

回答by elclanrs

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

它应该可以正常工作,问题是您使用了错误的字符,ndash与连字符-

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

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

如果你复制/粘贴,你会看到它现在可以工作。