我如何检查如果一个变量是一个整数在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>
当前回答
接受的答案不为我工作,因为我需要检查int/float和字母。因此,尝试这将工作的int/float和字母检查
function is_int(value){
if( (parseInt(value) % 1 === 0 )){
return true;
}else{
return false;
}
}
使用
is_int(44); // true
is_int("44"); // true
is_int(44.55); // true
is_int("44.55"); // true
is_int("aaa"); // false
其他回答
检查变量是否等于相同的变量四舍五入为整数,就像这样:
if(Math.round(data) != data) {
alert("Variable is not an integer!");
}
你可以tryNumber.isInteger(Number(value))如果value可能是一个字符串形式的整数,例如var value = "23",你想要这个值为true。避免尝试Number.isInteger(parseInt(value)),因为这并不总是返回正确的值。例如,如果var value = "23abc",并且你使用parseInt实现,它仍然会返回true。
但是如果你想要严格的整数值,那么可能Number.isInteger(value)就可以了。
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 (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
好的,得到负号,因为没有描述我的例子,所以更多的例子:):
我使用正则表达式和测试方法:
var isInteger = /^[0-9]\d*$/;
isInteger.test(123); //true
isInteger.test('123'); // true
isInteger.test('sdf'); //false
isInteger.test('123sdf'); //false
// If u want to avoid string value:
typeof testVal !== 'string' && isInteger.test(testValue);