如何将空字符串的 NaN 从 parseInt 转换为 0?
在 JavaScript 中解析值时是否可以以某种方式返回 0 而不是 NaN?
如果是空字符串,parseInt
返回 NaN
。
是否可以在 JavaScript 中执行类似的操作来检查 NaN
?
var value = parseInt(tbb) == NaN ? 0 : parseInt(tbb)
或者也许还有另一个函数或 jQuery 插件可以做类似的事情?
Is it possible somehow to return 0 instead of NaN
when parsing values in JavaScript?
In case of the empty string parseInt
returns NaN
.
Is it possible to do something like that in JavaScript to check for NaN
?
var value = parseInt(tbb) == NaN ? 0 : parseInt(tbb)
Or maybe there is another function or jQuery plugin which may do something similar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
当不与布尔值一起使用时,逻辑 OR
||
运算符返回第一个表达式parseInt(s)
(如果它可以计算为true
),否则返回第二个表达式0
。parseInt('')
的返回值为NaN
。NaN
计算结果为false
,因此num
最终被设置为0
。When not used with boolean values, the logical OR
||
operator returns the first expressionparseInt(s)
if it can be evaluated totrue
, otherwise it returns the second expression0
. The return value ofparseInt('')
isNaN
.NaN
evaluates tofalse
, sonum
ends up being set to0
.您还可以使用 isNaN() 函数:
You can also use the
isNaN()
function:问题
其他答案没有考虑到
0
是假的,因此以下将是20而不是0:解决方案
我提出一个辅助函数,可以解决大多数问题:
辅助函数将给出以下结果:
The problem
Other answers don't take into account that
0
is falsy, and thus the following will be 20 instead of 0:The solution
I propose a helper function, that solves most of the issues:
The helper function will give the following results:
我很惊讶没有人提到使用
Number()
。当然,如果提供的话,它会解析小数,因此其行为与 parseInt() 不同,但它已经假定基数为 10,并且会将 "" 甚至 " " 转换为 0。I was surprised to not see anyone mention using
Number()
. Granted it will parse decimals if provided, so will act differently thanparseInt()
, however it already assumes base 10 and will turn "" or even " " in to 0.对于不限于
parseInt
的人,您可以使用按位 OR 运算符(隐式调用ToInt32
到其操作数)。注意:
ToInt32
与parseInt
不同。 (即parseInt('33Ab') === 33
)For people who are not restricted to
parseInt
, you can use the bitwise OR operator (which implicitly callsToInt32
to its operands).Note:
ToInt32
is different fromparseInt
. (i.e.parseInt('33Ab') === 33
)在我看来,这项工作比 parseInt 干净得多,使用 + 运算符
Does the job a lot cleaner than parseInt in my opinion, Use the +operator
为什么不重写这个函数呢?在这种情况下,您始终可以确保在
NaN
的情况下它返回0
:现在,在代码中的任何位置:
Why not override the function? In that case you can always be sure it returns
0
in case ofNaN
:Now, anywhere in your code:
对于其他寻找此解决方案的人,只需使用: ~~ 而不使用 parseInt,这是最干净的模式。
如果为 NaN,则返回 0。
For other people looking for this solution, just use: ~~ without parseInt, it is the cleanest mode.
If NaN, it will return 0 instead.
您可以拥有非常干净的代码,我遇到了类似的问题,我通过使用解决了它:
You can have very clean code, i had similar problems and i solved it by using :
对空字符串进行单独检查(因为它是一种特定情况),并在这种情况下将其设置为零。
您可以在开头附加“0”,但随后您需要添加前缀以表明它是十进制而不是八进制数
Do a separate check for an empty string ( as it is one specific case ) and set it to zero in this case.
You could appeand "0" to the start, but then you need to add a prefix to indicate that it is a decimal and not an octal number
我有一个类似的问题(firefox v34),简单的字符串如下:
所以我想出了一个快速破解:
然后让所有愚蠢的复杂处理浮点数+整数用于非简单的东西:
它将返回 0 对于类似的事情:
I had a similar problem (firefox v34) with simple strings like:
So I came up with a quick hack of:
And then got all stupid complicated to deal with floats + ints for non-simple stuff:
It will return 0 for things like:
我创建了一个 2 原型来为我处理这个问题,一个用于数字,一个用于字符串。
如果您不想使用安全检查,请使用
示例用法:
I created a 2 prototype to handle this for me, one for a number, and one for a String.
If you dont want to use the safety check, use
Example usage:
同样这样,为什么不编写一个函数并在需要时调用它呢?我假设它是进入表单字段以执行计算的内容。
这样做有什么问题吗?
Also this way, why not write a function and call it where ever required . I'm assuming it's the entry into the form fields to perform calculations.
what's wrong doing this?
这是我正在使用的 tryParseInt 方法,它将默认值作为第二个参数,因此它可以是您需要的任何内容。
Here is a tryParseInt method that I am using, this takes the default value as second parameter so it can be anything you require.
仍然允许使用基数的辅助函数
an helper function which still allow to use the radix
对于那些不觉得它微不足道的人来说,解释一下:
乘以一,一种称为“隐式转换”的方法,尝试将未知类型操作数转换为原始类型“数字”。特别是,一个空字符串将变成数字 0,使其成为 parseInt() 的合格类型...
PirateApp 上面也给出了一个很好的例子,他建议在前面添加 + 号,强制 JavaScript 使用 Number 隐式强制转换。
8 月 20 日更新:
parseInt("0"+expr);
提供更好的结果,特别是parseInt("0"+'str');
Explanation, for those who don't find it trivial:
Multiplying by one, a method called "implicit cast", attempts to turn the unknown type operand into the primitive type 'number'. In particular, an empty string would become number 0, making it an eligible type for parseInt()...
A very good example was also given above by PirateApp, who suggested to prepend the + sign, forcing JavaScript to use the Number implicit cast.
Aug. 20 update:
parseInt("0"+expr);
gives better results, in particular forparseInt("0"+'str');