如何发现一个数字是浮点数或整数?
1.25 --> float
1 --> integer
0 --> integer
0.25 --> float
如何发现一个数字是浮点数或整数?
1.25 --> float
1 --> integer
0 --> integer
0.25 --> float
当前回答
function isInt(n)
{
return n != "" && !isNaN(n) && Math.round(n) == n;
}
function isFloat(n){
return n != "" && !isNaN(n) && Math.round(n) != n;
}
适用于所有情况。
其他回答
它真的不必那么复杂。整数的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) }
比较floor()结果与ceil()结果不同。
const isFloat = v => Math.floor(v) !== Math.ceil(v);
> isFloat(1)
= false
> isFloat(1.1)
= true
> isFloat(42)
= false
> isFloat(84.42)
= true
在这里尝试了一些答案,我最终写出了这个解决方案。这也适用于字符串中的数字。
function isInt(number) {
if(!/^["|']{0,1}[-]{0,1}\d{0,}(\.{0,1}\d+)["|']{0,1}$/.test(number)) return false;
return !(number - parseInt(number));
}
function isFloat(number) {
if(!/^["|']{0,1}[-]{0,1}\d{0,}(\.{0,1}\d+)["|']{0,1}$/.test(number)) return false;
return number - parseInt(number) ? true : false;
}
var测试={“integer”:1,“浮动”:1.1,“整数InString”:“5”,“floatInString”:“5.5”,“负Int”:-345,“负浮动”:-34.98,“negativeIntString”:“-45”,“negativeFloatString”:“-23.09”,“notValidFalse”:false,“notValidTrue”:true,“notValidString”:“45lorem”,“notValidStringFloat”:“4.5lorem”,'notValidNan':NaN,“notValidObj”:{},“notValidArr”:[1,2],};函数isInt(数字){如果(!/^[“|']{0,1}[-]{0.1}\d{0,}(\.{0,1}\d+)[“|']{0 1}$/.test(数字))返回false;回来(number-parseInt(number));}函数isFloat(数字){如果(!/^[“|']{0,1}[-]{0.1}\d{0,}(\.{0,1}\d+)[“|']{0 1}$/.test(数字))返回false;返回number-parseInt(number)?真:假;}函数测试函数(obj){var keys=对象.keys(obj);var values=对象.值(obj);values.forEach(函数(元素,索引){console.log(`${keys[index]}(${element})var是整数吗${isInt(元素)}`);console.log(`${keys[index]}(${element})var是浮点数吗${isFloat(元素)}`);});}testFunctions(测试);
可以使用简单的正则表达式:
function isInt(value) {
var er = /^-?[0-9]+$/;
return er.test(value);
}
或者,您也可以根据需要使用以下功能。它们由PHPJS项目开发。
is_int()=>检查变量类型是否为整数,其内容是否为整数
is_float()=>检查变量类型是否为float,其内容是否为floate
ctype_digit()=>检查变量类型是否为字符串,以及其内容是否只有小数位数
更新1
现在它也检查负数,感谢@ChrisBartley的评论!
使用此选项,您可以检查字符串或数字是否为“十进制”(正确浮动):
var IsDecimal = function(num){
return ((num.toString().split('.').length) <= 2 && num.toString().match(/^[\+\-]?\d*\.?\d+(?:[Ee][\+\-]?\d+)?$/)) ? (!isNaN(Number.parseFloat(num))) : false ;
}
另一个用于检查字符串或数字是否为整数:
var IsInteger = function(num){
return ((num.toString().split('.').length) == 1 && num.toString().match(/^[\-]?\d+$/)) ? (!isNaN(Number.parseInt(num))) : false ;
}
var IsDecimal=函数(num){return((num.toString().split('.').length)<=2&&num.toSString().match(/^[\+\-]?\d*\.?\d+(?:[Ee][\+\-]?\d+)?$/))?(!isNaN(Number.parseFloat(num)):false;}var IsInteger=函数(num){return((num.toString().split('.').length)==1&num.toSString().match(/^[\-]?\d+$/))?(!isNaN(Number.parseInt(num)):false;}console.log(“--------------作为字符串--------------”);console.log(“整数:”);console.log(“0=”+IsInteger(“0”));console.log(“34=”+IsInteger(“34”));console.log(“.34=”+IsInteger(“.34”));console.log(“3.4=”+IsInteger(“3.4”));console.log(“3e=”+IsInteger(“3e”));console.log(“e3=”+IsInteger(“e3”));console.log(“-34=”+IsInteger(“-34”));console.log(“--34=”+IsInteger(“--34”));console.log(“034=”+IsInteger(“034”));console.log(“0-34=”+IsInteger(“0-34”));console.log(“浮点/小数:”);console.log(“0=”+IsDecimal(“0”));console.log(“64=”+IsDecimal(“64”));console.log(“.64=”+IsDecimal(“.64”));console.log(“6.4=”+IsDecimal(“6.4”));console.log(“6e2=”+IsDecimal(“6e2”));console.log(“6e=”+IsDecimal(“6e”));console.log(“e6=”+IsDecimal(“e6”));console.log(“-64=”+IsDecimal(“-64”));console.log(“--64=”+IsDecimal(“--64”));console.log(“064=”+IsDecimal(“064”));console.log(“0-64=”+IsDecimal(“0-64”));console.log(“\n--------------作为数字--------------”);console.log(“整数:”);console.log(“0=”+IsInteger(0));console.log(“34=”+IsInteger(34));console.log(“.34=”+IsInteger(0.34));console.log(“3.4=”+IsInteger(3.4));console.log(“-34=”+IsInteger(-34));console.log(“034=”+IsInteger(034));console.log(“0-34=”+IsInteger(0-34));console.log(“浮点/小数:”);console.log(“0=”+IsDecimal(0));console.log(“64=”+IsDecimal(64));console.log(“.64=”+IsDecimal(0.64));console.log(“6.4=”+IsDecimal(6.4));console.log(“6e2=”+IsDecimal(6e2));console.log(“-64=”+IsDecimal(-64));console.log(“064=”+IsDecimal(064));console.log(“0-64=”+IsDecimal(0-64));