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

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

当前回答

这个解决方案对我有效。

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

其他回答

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

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

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

以下是我对整数的用法:

Math.ceil(parseFloat(val)) === val

很短,很好:)一直工作。如果我没弄错的话,这就是大卫·弗拉纳根的建议。

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

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

我需要检查一个输入值,如果它是整数或浮点值,为此我想出了以下方法:

函数isInteger(x){var整数=parseInt(x,10);如果(!isNaN(整数)&&!isFloat(x)){返回true;}return false;}函数isFloat(x){var f=parseFloat(x);var floor=数学楼层(f);var分数=f-地板;如果(分数>0){返回true;}return false;}var案例=["1","1.00","1.01","0.05",“ab1”,“ab1.1”,1.1.00,1.01,0.05,1e+5,"",真的,假,无效的NaN,未定义,];console.log(“isInteger()”);对于(var i=0;i<cases.length;i++){console.log(cases[i],isInteger(cases[i]));}console.log(“\nisFloat()”);对于(var i=0;i<cases.length;i++){console.log(cases[i],isFloat(cases[i]));}

function isInteger(n) {
   return ((typeof n==='number')&&(n%1===0));
}

function isFloat(n) {
   return ((typeof n==='number')&&(n%1!==0));
}

function isNumber(n) {
   return (typeof n==='number');
}