我如何检查如果一个变量是一个整数在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>
当前回答
大整数(bigint)呢?
大多数这些答案在大整数(253或更大)上失败:(x | 0) === x),测试typeof x === 'number',常规int函数(例如parseInt),常规算术在大整数上失败。这可以通过使用BigInt来解决。
我将几个答案编译成一个代码片段以显示结果。对于大整数,大多数完全失败,而其他的则可以,除非传入BigInt类型(例如1n)。我没有包括重复的答案,也没有任何答案,允许小数或不试图测试类型)
// these all fail n = 1000000000000000000000000000000 b = 1n // These all fail on large integers //https://stackoverflow.com/a/14636652/3600709 console.log('fail',1,n === parseInt(n, 10)) //https://stackoverflow.com/a/14794066/3600709 console.log('fail',2,!isNaN(n) && parseInt(Number(n)) == n && !isNaN(parseInt(n, 10))) console.log('fail',2,!isNaN(n) && (parseFloat(n) | 0) === parseFloat(n)) console.log('fail',2,!isNaN(n) && (function(x) { return (x | 0) === x; })(parseFloat(n))) //https://stackoverflow.com/a/21742529/3600709 console.log('fail',3,n == ~~n) //https://stackoverflow.com/a/28211631/3600709 console.log('fail',4,!isNaN(n) && parseInt(n) == parseFloat(n)) //https://stackoverflow.com/a/41854178/3600709 console.log('fail',5,String(parseInt(n, 10)) === String(n)) // These ones work for integers, but not BigInt types (e.g. 1n) //https://stackoverflow.com/a/14636725/3600709 console.log('partial',1,typeof n==='number' && (n%1)===0) // this one works console.log('partial',1,typeof b==='number' && (b%1)===0) // this one fails //https://stackoverflow.com/a/27424770/3600709 console.log('partial',2,Number.isInteger(n)) // this one works console.log('partial',2,Number.isInteger(b)) // this one fails //https://stackoverflow.com/a/14636638/3600709 console.log('partial',3,n % 1 === 0) console.log('partial',3,b % 1 === 0) // gives uncaught type on BigInt
检查类型
如果你真的想测试输入值的类型,以确保它是一个整数,可以使用下面的方法:
function isInt(value) {
try {
BigInt(value)
return !['string','object','boolean'].includes(typeof value)
} catch(e) {
return false
}
}
function isInt(value) { try { BigInt(value) return !['string','object','boolean'].includes(typeof value) } catch(e) { return false } } console.log('--- should be false') console.log(isInt(undefined)) console.log(isInt('')) console.log(isInt(null)) console.log(isInt({})) console.log(isInt([])) console.log(isInt(1.1e-1)) console.log(isInt(1.1)) console.log(isInt(true)) console.log(isInt(NaN)) console.log(isInt('1')) console.log(isInt(function(){})) console.log(isInt(Infinity)) console.log('--- should be true') console.log(isInt(10)) console.log(isInt(0x11)) console.log(isInt(0)) console.log(isInt(-10000)) console.log(isInt(100000000000000000000000000000000000000)) console.log(isInt(1n))
不检查类型
如果你不关心传入类型是否实际上是布尔值、字符串等转换成数字,那么只需使用以下方法:
function isInt(value) {
try {
BigInt(value)
return true
} catch(e) {
return false
}
}
function isInt(value) { try { BigInt(value) return true } catch(e) { return false } } console.log('--- should be false') console.log(isInt(undefined)) console.log(isInt(null)) console.log(isInt({})) console.log(isInt(1.1e-1)) console.log(isInt(1.1)) console.log(isInt(NaN)) console.log(isInt(function(){})) console.log(isInt(Infinity)) console.log('--- should be true') console.log(isInt(10)) console.log(isInt(0x11)) console.log(isInt(0)) console.log(isInt(-10000)) console.log(isInt(100000000000000000000000000000000000000)) console.log(isInt(1n)) // gets converted to number console.log(isInt('')) console.log(isInt([])) console.log(isInt(true)) console.log(isInt('1'))
其他回答
在经历了几次成功和失败之后,我想出了这个解决方案:
const isInt = (value) => {
return String(parseInt(value, 10)) === String(value)
}
我喜欢上面的想法,检查不是NaN的值并使用parseFloat,但当我在React基础设施中尝试它时,由于某些原因它不起作用。
编辑: 我找到了一个不用字符串的更好的方法:
var isInt = function (str) {
return str === '0' || !!~~str;
}
我认为这是最简短的答案。也许是最有效的,但我可以更正一下。:)
对于没有分隔符的正整数值:
return ( data !== '' && data === data.replace(/\D/, '') );
测试 1. 如果不是空的, 2. 如果value等于在其值中替换非数字字符的结果。
好的,得到负号,因为没有描述我的例子,所以更多的例子:):
我使用正则表达式和测试方法:
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);
你可以使用这个函数:
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
如果你的浏览器支持,Number.isInteger()是最好的方法,如果不支持,我认为有很多方法:
function isInt1(value){
return (value^0) === value
}
or:
function isInt2(value){
return (typeof value === 'number') && (value % 1 === 0);
}
or:
function isInt3(value){
return parseInt(value, 10) === value;
}
or:
function isInt4(value){
return Math.round(value) === value;
}
现在我们可以测试结果:
var value = 1
isInt1(value) // return true
isInt2(value) // return true
isInt3(value) // return true
isInt4(value) // return true
var value = 1.1
isInt1(value) // return false
isInt2(value) // return false
isInt3(value) // return false
isInt4(value) // return false
var value = 1000000000000000000
isInt1(value) // return false
isInt2(value) // return true
isInt3(value) // return false
isInt4(value) // return true
var value = undefined
isInt1(value) // return false
isInt2(value) // return false
isInt3(value) // return false
isInt4(value) // return false
var value = '1' //number as string
isInt1(value) // return false
isInt2(value) // return false
isInt3(value) // return false
isInt4(value) // return false
所有这些方法都可以,但当数字很大时,parseInt和^ operator就不能很好地工作了。