本文介绍了较大的负数上的Javascript parseInt给出NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我这样做时:

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

我得到的值是:

NaN

为什么会这样?

我想测试数字是否在C(int)范围内,所以我在做上面的事情,但是不起作用.另外,我想对C(长整数)执行此操作,有办法吗?

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

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

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?

推荐答案

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

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.

这篇关于较大的负数上的Javascript parseInt给出NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 12:37