我如何检查如果一个变量是一个整数在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 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);
}
当然了,我建议让这个小工具来处理。
其他回答
Number.isInteger()似乎是可行的方法。
MDN还为不支持Number.isInteger()的浏览器提供了以下填充,主要是所有版本的IE。
链接到MDN页面
Number.isInteger = Number.isInteger || function(value) {
return typeof value === "number" &&
isFinite(value) &&
Math.floor(value) === value;
};
检查变量是否等于相同的变量四舍五入为整数,就像这样:
if(Math.round(data) != data) {
alert("Variable is not an integer!");
}
假设你对问题中的变量一无所知,你应该采用这种方法:
if(typeof data === 'number') {
var remainder = (data % 1);
if(remainder === 0) {
// yes, it is an integer
}
else if(isNaN(remainder)) {
// no, data is either: NaN, Infinity, or -Infinity
}
else {
// no, it is a float (still a number though)
}
}
else {
// no way, it is not even a number
}
简单来说:
if(typeof data==='number' && (data%1)===0) {
// data is an integer
}
试试下面的功能:
function isInteger (num) {
return num == parseInt(+num,10) && !isNaN(parseInt(num));
}
console.log ( isInteger(42)); // true
console.log ( isInteger("42")); // true
console.log ( isInteger(4e2)); // true
console.log ( isInteger("4e2")); // true
console.log ( isInteger(" 1 ")); // true
console.log ( isInteger("")); // false
console.log ( isInteger(" ")); // false
console.log ( isInteger(42.1)); // false
console.log ( isInteger("1a")); // false
console.log ( isInteger("4e2a")); // false
console.log ( isInteger(null)); // false
console.log ( isInteger(undefined)); // false
console.log ( isInteger(NaN)); // false
console.log ( isInteger(false)); // false
console.log ( isInteger(true)); // false
console.log ( isInteger(Infinity)); // false
为什么没有人提到Number.isInteger() ?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger
为我完美地工作,并解决了NaN开始一个数字的问题。