如何发现一个数字是浮点数或整数?
1.25 --> float
1 --> integer
0 --> integer
0.25 --> float
如何发现一个数字是浮点数或整数?
1.25 --> float
1 --> integer
0 --> integer
0.25 --> float
当前回答
对于那些好奇的人,我使用Benchmark.js测试了这篇文章中投票最多的答案(以及今天发布的答案),以下是我的结果:
var n = -10.4375892034758293405790;
var suite = new Benchmark.Suite;
suite
// kennebec
.add('0', function() {
return n % 1 == 0;
})
// kennebec
.add('1', function() {
return typeof n === 'number' && n % 1 == 0;
})
// kennebec
.add('2', function() {
return typeof n === 'number' && parseFloat(n) == parseInt(n, 10) && !isNaN(n);
})
// Axle
.add('3', function() {
return n.toString().indexOf('.') === -1;
})
// Dagg Nabbit
.add('4', function() {
return n === +n && n === (n|0);
})
// warfares
.add('5', function() {
return parseInt(n) === n;
})
// Marcio Simao
.add('6', function() {
return /^-?[0-9]+$/.test(n.toString());
})
// Tal Liron
.add('7', function() {
if ((undefined === n) || (null === n)) {
return false;
}
if (typeof n == 'number') {
return true;
}
return !isNaN(n - 0);
});
// Define logs and Run
suite.on('cycle', function(event) {
console.log(String(event.target));
}).on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
}).run({ 'async': true });
0 x 12,832,357 ops/sec ±0.65% (90 runs sampled)
1 x 12,916,439 ops/sec ±0.62% (95 runs sampled)
2 x 2,776,583 ops/sec ±0.93% (92 runs sampled)
3 x 10,345,379 ops/sec ±0.49% (97 runs sampled)
4 x 53,766,106 ops/sec ±0.66% (93 runs sampled)
5 x 26,514,109 ops/sec ±2.72% (93 runs sampled)
6 x 10,146,270 ops/sec ±2.54% (90 runs sampled)
7 x 60,353,419 ops/sec ±0.35% (97 runs sampled)
Fastest is 7 Tal Liron
其他回答
这个解决方案对我有效。
<html>
<body>
<form method="post" action="#">
<input type="text" id="number_id"/>
<input type="submit" value="send"/>
</form>
<p id="message"></p>
<script>
var flt=document.getElementById("number_id").value;
if(isNaN(flt)==false && Number.isInteger(flt)==false)
{
document.getElementById("message").innerHTML="the number_id is a float ";
}
else
{
document.getElementById("message").innerHTML="the number_id is a Integer";
}
</script>
</body>
</html>
这是我能想到的浮点和整数检查的最佳解决方案。
函数isFloat(n){如果(!n){return false}回来isNaN(n%1)&&n%1!==0;}函数isInt(n){如果(n长度==0){return false}回来isNaN(n%1)&&n%1==0;}
基于我在这里看到的所有内容,我创建了自己的一组函数来测试我所需要的:
function NumberValidator() {
this.isFloat = function (n) {
return typeof(n)==="number" && n === +n && Math.round(n) !== n;
};
this.isInteger = function (n) {
return typeof(n)==="number" && n === +n && Math.round(n) === n;
};
this.isFloatOrInteger = function (n) {
return this.isFloat(n) || this.isInteger(n);
};
this.isNonZeroFloatOrInteger = function (n) {
return this.isFloatOrInteger(n) && n > 0;
};
this.isNonZeroInteger = function (n) {
return this.isInteger(n) && n > 0;
};
}
然而,shime的解决方案更短,检查更少,因此可能是更好的解决方案。
它真的不必那么复杂。整数的parseFloat()和parseInt()等价物的数值将相同。因此,您可以这样做:
function isInt(value){
return (parseFloat(value) == parseInt(value)) && !isNaN(value);
}
Then
if (isInt(x)) // do work
这也将允许字符串检查,因此并不严格。如果想要一个强类型的解决方案(也就是,不使用字符串):
function is_int(value){ return !isNaN(parseInt(value * 1) }
以下是我对整数的用法:
Math.ceil(parseFloat(val)) === val
很短,很好:)一直工作。如果我没弄错的话,这就是大卫·弗拉纳根的建议。