如何发现一个数字是浮点数或整数?

1.25 --> float  
1 --> integer  
0 --> integer  
0.25 --> float

当前回答

简单整数测试:

if( n === parseInt(n) ) ...

有意义:如果JavaScript可以将某个东西转换为整数,并且通过转换它变成完全相同的东西,那么操作数就是整数。

控制台测试用例:

x = 1;     x===parseInt(x); // true
x = "1";   x===parseInt(x); // false
x = 1.1;   x===parseInt(x); // false, obviously

// BUT!

x = 1.0;   x===parseInt(x); // true, because 1.0 is NOT a float!

这让很多人困惑。每当某个值为0时,它就不再是浮球了。这是一个整数。或者你可以把它称为“一个数字的东西”,因为没有像当时的C那样严格的区分。

所以基本上,你所能做的就是检查整数,接受1.000是整数的事实。

有趣的侧记

有人评论说数字巨大。巨大的数字意味着这种方法没有问题;每当parseInt无法处理该数字(因为它太大)时,它将返回实际值以外的其他值,因此测试将返回FALSE。看:

var a = 99999999999999999999;
var b = 999999999999999999999; // just one more 9 will kill the show!

var aIsInteger = (  a===parseInt(a)  )?"a is ok":"a fails";
var bIsInteger = (  b===parseInt(b)  )?"b is ok":"b fails";

alert(aIsInteger+"; "+bIsInteger);

2014年,我在IE8上测试了这一点,然后在Chrome上测试了2021,两者都返回“a是ok;b是fails”,这意味着如果一个数字太大,它就不能再是整数了。

引用一句经典的话,20位数字对任何人来说都应该足够了。

其他回答

它真的不必那么复杂。整数的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) }

我知道已经有30个答案,但一个复杂的方法是这样做:

function isInteger(n) {
    return n.toString().split('.').length === 1;
}

解释:我们首先将n转换为字符串,然后基于点拆分它。如果n是浮点,如4.5,则拆分将返回数组['4','5']。如果它是像45这样的整数,它将返回['45']。因此,如果数组的长度是1,那么我们知道它是一个数字。

注:如果您想以新的ES6格式(箭头函数)编写此函数:

const isInteger = n => n.toString().split('.').length === 1;

您可以使用Number.isInteger()方法通过对数字进行除法来检查数字是整数还是浮点数,例如:

    function isNumberFloatOrInteger(a, b){
     if(Number.isInteger(a / b)){
      return true;
      }
      else{ return false };
}

注意:isInteger()与internet explorer不兼容。

当除以1时检查余数:

function isInt(n) {
   return n % 1 === 0;
}

如果你不知道参数是一个数字,你需要两个测试:

function isInt(n){
    return Number(n) === n && n % 1 === 0;
}

function isFloat(n){
    return Number(n) === n && n % 1 !== 0;
}

2019年更新在这个答案写出来5年后,一个解决方案在ECMA脚本2015中被标准化。这个答案涵盖了这个解决方案。

使用此选项,您可以检查字符串或数字是否为“十进制”(正确浮动):

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));