将String变量的值转换为int/数字变量的正确方法是什么?为什么bcInt
仍然是string,而isNaN
返回true
?
bc=localStorage.getItem('bc');
var bcInt=parseInt(bc,10);
var bcInt2=1;
console.log("bc------------>" +bc +" isNaN:" +isNaN(bc)); //isNaN returns true
console.log("bcInt------------>" +bcInt +" isNaN:" +isNaN(bcInt)); //isNaN returns true
bcInt2// isNaN returns false
parseInt()
返回NaN
,那么您的字符串实际上不包含值的数字表示。如果您已经告诉我们“bc”的值,也许有人可以帮忙解决问题,但是您没有提供那个信息。当然,这个值是解决整个问题的关键。 - Pointy