我只在Firefox的JavaScript控制台中尝试过,但以下语句都没有返回true:
parseFloat('geoff') == NaN;
parseFloat('geoff') == Number.NaN;
我只在Firefox的JavaScript控制台中尝试过,但以下语句都没有返回true:
parseFloat('geoff') == NaN;
parseFloat('geoff') == Number.NaN;
当前回答
根据IEEE 754,所有涉及NaN的关系都被评估为假,除了!=。因此,例如,如果A或B或两者都是NaN, (A >= B) = false且(A <= B) = false。
其他回答
虽然@chiborg的回答是正确的,但还有更多需要注意的地方:
parseFloat('1.2geoff'); // => 1.2
isNaN(parseFloat('1.2geoff')); // => false
isNaN(parseFloat('.2geoff')); // => false
isNaN(parseFloat('geoff')); // => true
重点是,如果你使用这个方法来验证输入,结果将是相当自由的。
所以,是的,你可以使用parseFloat(string)(或在完整数字的情况下parseInt(string, radix)',然后随后用isNaN()包装它,但要注意数字与其他非数字字符交织在一起的问题。
alert("1234567890.".indexOf(String.fromCharCode(mycharacter))>-1);
这并不优雅。但在尝试isNAN()后,我得到了这个解决方案,这是另一种选择。在这个例子中,我还允许'。'因为我正在为浮动进行屏蔽。您还可以反转此操作,以确保不使用任何数字。
("1234567890".indexOf(String.fromCharCode(mycharacter))==-1)
这是一个单个字符的求值,但您也可以循环遍历字符串以检查任何数字。
marksyzm的答案工作得很好,但它不会为无穷大返回false,因为无穷大在技术上不是一个数字。
我想出了一个isNumber函数来检查它是否是一个数字。
函数isNumber(i) isNaN(i && i !== true ?编号(i): parseFloat(i)) &&[编号。POSITIVE_INFINITY, Number.NEGATIVE_INFINITY].indexOf(i) == -1; } console.log (isNumber(∞)); console.log (isNumber(“asdf ")); console.log (isNumber (1.4)); console.log (isNumber(南)); console.log (isNumber (Number.MAX_VALUE)); console.log (isNumber (" 1.68 "));
更新: 我注意到这段代码在某些参数上失败了,所以我改进了它。
function isNumber(i) {//function for checking if parameter is number if(!arguments.length) { throw new SyntaxError("not enough arguments."); } else if(arguments.length > 1) { throw new SyntaxError("too many arguments."); } else if([Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY].indexOf(i) !== -1) { throw new RangeError("number cannot be \xB1infinity."); } else if(typeof i === "object" && !(i instanceof RegExp) && !(i instanceof Number) && !(i === null)) { throw new TypeError("parameter cannot be object/array."); } else if(i instanceof RegExp) { throw new TypeError("parameter cannot be RegExp."); } else if(i == null || i === undefined) { throw new ReferenceError("parameter is null or undefined."); } else { return !isNaN(i && i !== true ? Number(i) : parseFloat(i)) && (i === i); } } console.log(isNumber(Infinity)); console.log(isNumber(this)); console.log(isNumber(/./ig)); console.log(isNumber(null));
MDN的parseFloat页面中提到了另一个解决方案
它提供了一个过滤器函数来执行严格的解析
var filterFloat = function (value) {
if(/^(\-|\+)?([0-9]+(\.[0-9]+)?|Infinity)$/
.test(value))
return Number(value);
return NaN;
}
console.log(filterFloat('421')); // 421
console.log(filterFloat('-421')); // -421
console.log(filterFloat('+421')); // 421
console.log(filterFloat('Infinity')); // Infinity
console.log(filterFloat('1.61803398875')); // 1.61803398875
console.log(filterFloat('421e+0')); // NaN
console.log(filterFloat('421hop')); // NaN
console.log(filterFloat('hop1.61803398875')); // NaN
然后你可以用isNaN来检查它是否是NaN
规则是:
NaN != NaN
isNaN()函数的问题是,在某些情况下,它可能会返回意想不到的结果:
isNaN('Hello') //true
isNaN('2005/12/12') //true
isNaN(undefined) //true
isNaN('NaN') //true
isNaN(NaN) //true
isNaN(0 / 0) //true
检查该值是否真的为NaN的更好方法是:
function is_nan(value) {
return value != value
}
is_nan(parseFloat("geoff"))