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

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

当前回答

这真的取决于你想要实现什么。如果你想“模仿”强类型语言,那么我建议你不要尝试。正如其他人提到的,所有数字都有相同的表示(相同类型)。

使用Claudiu提供的内容:

isInteger(1.0)->true

这看起来很正常,但在C这样的情况下,你会错

其他回答

下面的函数防止空字符串、未定义、空值和max/min值范围。Javascript引擎从一开始就应该内置这些函数。:)

享受

function IsInteger(iVal) {
    var iParsedVal; //our internal converted int value


    iParsedVal = parseInt(iVal,10);

    if (isNaN(iParsedVal) || Infinity == iParsedVal || -Infinity == iParsedVal) //sanity check - guard against empty strings and max/min values
        return false;
    else
        return Number(iVal) === (iParsedVal | 0); //the 2nd operand group (intValue | 0), evaluates to true only if the intValue is an integer; so an int type will only return true
}

function IsFloat(fVal) {
    var fParsedVal; //our internal converted float value


    fParsedVal = parseFloat(fVal);

    if (isNaN(fParsedVal) || Infinity == fParsedVal || -Infinity == fParsedVal) //sanity check - guard against empty strings and max/min values
        return false;
    else
        return !!(fVal % 1); //true only if there is a fractional value after the mod op; the !! returns the opposite value of the op which reflects the function's return value
}

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

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

为什么不这样做:

var isInt = function(n) { return parseInt(n) === n };
function int(a) {
  return a - a === 0 && a.toString(32).indexOf('.') === -1
}

function float(a) {
  return a - a === 0 && a.toString(32).indexOf('.') !== -1
}

如果要排除字符串,可以添加类型a==“number”。

另一种方法是:

    function isFloat(float) {
        return /\./.test(float.toString());
    }

可能没有其他方法有效,但另一种方法仍然有效。