为什么 parseInt 0.11 不是 NaN 而是 0?
Why parseInt 0.11 is not NaN but 0?
这正常吗?
newItemRowNumber
'0.11'
parseInt(newItemRowNumber)
0
我希望它不可解析。
parseInt 解析字符串开头的实数 0-9。当遇到非整数字符时,它会停止解析,在本例中为 .
19aaa 变成 19
0.11 变成 0
11.111 变成 11
abc11 变为 NaN
0xDEAD变成57005(因为十六进制数)
来自MDN:
If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values. Leading and trailing spaces are allowed.
0
是一个数字。
.
不是。
所以它接受 0
,忽略 .
,忽略 .
之后的所有内容,然后你得到 0
。
这是正常的,因为 parseInt 只会从给定的数字字符串中删除小数部分。和 return 点左侧数字的 int 值。
这里是 Link parseInt() in JS.
当你解析浮点数时,你可以使用 parseFloat()
console.log(parseFloat("0.11"))
>>0.11
这正常吗?
newItemRowNumber
'0.11'
parseInt(newItemRowNumber)
0
我希望它不可解析。
parseInt 解析字符串开头的实数 0-9。当遇到非整数字符时,它会停止解析,在本例中为 .
19aaa 变成 19
0.11 变成 0
11.111 变成 11
abc11 变为 NaN
0xDEAD变成57005(因为十六进制数)
来自MDN:
If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values. Leading and trailing spaces are allowed.
0
是一个数字。
.
不是。
所以它接受 0
,忽略 .
,忽略 .
之后的所有内容,然后你得到 0
。
这是正常的,因为 parseInt 只会从给定的数字字符串中删除小数部分。和 return 点左侧数字的 int 值。 这里是 Link parseInt() in JS.
当你解析浮点数时,你可以使用 parseFloat()
console.log(parseFloat("0.11"))
>>0.11