我如何检查如果一个变量是一个整数在JavaScript,并抛出一个警告,如果它不是?我试过了,但不管用:
<html>
<head>
<script type="text/javascript">
var data = 22;
alert(NaN(data));
</script>
</head>
</html>
我如何检查如果一个变量是一个整数在JavaScript,并抛出一个警告,如果它不是?我试过了,但不管用:
<html>
<head>
<script type="text/javascript">
var data = 22;
alert(NaN(data));
</script>
</head>
</html>
当前回答
你可以使用这个函数:
function isInteger(value) {
return (value == parseInt(value));
}
即使值是包含整数值的字符串,它也将返回true。 因此,结果将是:
alert(isInteger(1)); // true
alert(isInteger(1.2)); // false
alert(isInteger("1")); // true
alert(isInteger("1.2")); // false
alert(isInteger("abc")); // false
其他回答
ecmascript -6之前最简单、最清晰的解决方案(它也足够健壮,即使传递给函数的是一个非数字值,如字符串或null,也会返回false)如下:
function isInteger(x) { return (x^0) === x; }
下面的解决方案也可以工作,尽管没有上面的那样优雅:
function isInteger(x) { return Math.round(x) === x; }
注意,在上述实现中Math.ceil()或Math.floor()也可以同样好地使用(而不是Math.round())。
或者:
function isInteger(x) { return (typeof x === 'number') && (x % 1 === 0); }
一个相当常见的错误解决方案是:
function isInteger(x) { return parseInt(x, 10) === x; }
While this parseInt-based approach will work well for many values of x, once x becomes quite large, it will fail to work properly. The problem is that parseInt() coerces its first parameter to a string before parsing digits. Therefore, once the number becomes sufficiently large, its string representation will be presented in exponential form (e.g., 1e+21). Accordingly, parseInt() will then try to parse 1e+21, but will stop parsing when it reaches the e character and will therefore return a value of 1. Observe:
> String(1000000000000000000000)
'1e+21'
> parseInt(1000000000000000000000, 10)
1
> parseInt(1000000000000000000000, 10) === 1000000000000000000000
false
function isInteger(argument) { return argument == ~~argument; }
用法:
isInteger(1); // true<br>
isInteger(0.1); // false<br>
isInteger("1"); // true<br>
isInteger("0.1"); // false<br>
or:
function isInteger(argument) { return argument == argument + 0 && argument == ~~argument; }
用法:
isInteger(1); // true<br>
isInteger(0.1); // false<br>
isInteger("1"); // false<br>
isInteger("0.1"); // false<br>
这要看情况,你是否也想将字符串转换为潜在整数?
这样做:
function isInt(value) {
return !isNaN(value) &&
parseInt(Number(value)) == value &&
!isNaN(parseInt(value, 10));
}
按位操作
简单的解析和检查
function isInt(value) {
var x = parseFloat(value);
return !isNaN(value) && (x | 0) === x;
}
短路并保存解析操作:
function isInt(value) {
if (isNaN(value)) {
return false;
}
var x = parseFloat(value);
return (x | 0) === x;
}
或者两者同时出现:
function isInt(value) {
return !isNaN(value) && (function(x) { return (x | 0) === x; })(parseFloat(value))
}
测试:
isInt(42) // true
isInt("42") // true
isInt(4e2) // true
isInt("4e2") // true
isInt(" 1 ") // true
isInt("") // false
isInt(" ") // false
isInt(42.1) // false
isInt("1a") // false
isInt("4e2a") // false
isInt(null) // false
isInt(undefined) // false
isInt(NaN) // false
这是小提琴:http://jsfiddle.net/opfyrqwp/28/
性能
测试表明,短路解决方案具有最佳的性能(ops/sec)。
// Short-circuiting, and saving a parse operation
function isInt(value) {
var x;
if (isNaN(value)) {
return false;
}
x = parseFloat(value);
return (x | 0) === x;
}
下面是一个基准: http://jsben.ch/#/htLVw
如果你喜欢更短的钝角短路形式:
function isInt(value) {
var x;
return isNaN(value) ? !1 : (x = parseFloat(value), (0 | x) === x);
}
当然了,我建议让这个小工具来处理。
从http://www.toptal.com/javascript/interview-questions:
function isInteger(x) { return (x^0) === x; }
我发现这是最好的方法。
检查像poster这样的整数是否需要:
if (+data===parseInt(data)) {return true} else {return false}
注意+在data前面(将字符串转换为数字),===表示精确。
下面是一些例子:
data=10
+data===parseInt(data)
true
data="10"
+data===parseInt(data)
true
data="10.2"
+data===parseInt(data)
false