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

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

当前回答

以下是检查值是否为数字或是否可以安全地转换为数字的有效函数:

function isNumber(value) {
    if ((undefined === value) || (null === value)) {
        return false;
    }
    if (typeof value == 'number') {
        return true;
    }
    return !isNaN(value - 0);
}

对于整数(如果值是浮点,则返回false):

function isInteger(value) {
    if ((undefined === value) || (null === value)) {
        return false;
    }
    return value % 1 == 0;
}

这里的效率是,当值已经是数字时,可以避免parseInt(或parseNumber)。这两个解析函数总是先转换为字符串,然后尝试解析该字符串,如果值已经是数字,这将是一种浪费。

感谢这里的其他帖子为优化提供了进一步的想法!

其他回答

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

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

在java脚本中,所有数字都是内部64位浮点,与java中的double相同。javascript中没有不同的类型,所有类型都由类型号表示。因此,您将无法进行检查实例。然而,你可以使用上面给出的解来确定它是否是一个分数。java脚本的设计者认为,只要使用一种类型,就可以避免大量的类型转换错误。

我知道已经有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;

我参加晚会迟到了,但这是我的版本

isInteger: obj => Number.isInteger(!isNaN(obj % 1) && obj % 1 !== 0 ? obj : parseInt(obj)),
!!(24%1) // false
!!(24.2%1) // true