如何检查数字是浮点数还是整数?

如何找到一个数字是floatinteger

1.25 --> float  
1 --> integer  
0 --> integer  
0.25 --> float
古一古一2020/03/11 12:30:23

Condtion for floating validation :

if (lnk.value == +lnk.value && lnk.value != (lnk.value | 0)) 

Condtion for Integer validation :

if (lnk.value == +lnk.value && lnk.value == (lnk.value | 0)) 

Hope this might be helpful.

Green小哥Tom2020/03/11 12:30:22

This solution worked for me.

<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>
乐小小2020/03/11 12:30:22

In java script all the numbers are internally 64 bit floating point, same as double in java. There are no diffrent types in javascript, all are represented by type number. Hence you wil l not be able make a instanceof check. However u can use the above solutions given to find out if it is a fractional number. designers of java script felt with a single type they can avoid numerous type cast errors.

三分糖就好2020/03/11 12:30:22

For integers I use this

function integer_or_null(value) {
    if ((undefined === value) || (null === value)) {
        return null;
    }
    if(value % 1 != 0) {
        return null;
    }
    return value;
}
达蒙JinJin2020/03/11 12:30:22
!!(24%1) // false
!!(24.2%1) // true
小胖逆天2020/03/11 12:30:22

How about this one?

isFloat(num) {
    return typeof num === "number" && !Number.isInteger(num);
}
路易猴子Pro2020/03/11 12:30:22

It really depends on what you want to achieve. If you want to "emulate" strongly typed languages then I suggest you not trying. As others mentioned all numbers have the same representation (the same type).

Using something like Claudiu provided:

isInteger( 1.0 ) -> true

which looks fine for common sense, but in something like C you would get false

达蒙老丝2020/03/11 12:30:22

Any Float number with a zero decimal part (e.g. 1.0, 12.00, 0.0) are implicitly cast to Integer, so it is not possible to check if they are Float or not.

小卤蛋Tony2020/03/11 12:30:22

As others mentioned, you only have doubles in JS. So how do you define a number being an integer? Just check if the rounded number is equal to itself:

function isInteger(f) {
    return typeof(f)==="number" && Math.round(f) == f;
}
function isFloat(f) { return typeof(f)==="number" && !isInteger(f); }
TonyDavaidLEY2020/03/11 12:30:22
function isInt(n) 
{
    return n != "" && !isNaN(n) && Math.round(n) == n;
}
function isFloat(n){
    return n != "" && !isNaN(n) && Math.round(n) != n;
}

适用于所有情况。

StafanL2020/03/11 12:30:22

为什么不这样:

var isInt = function(n) { return parseInt(n) === n };
猿老丝2020/03/11 12:30:22

您可以使用一个简单的正则表达式:

function isInt(value) {

    var er = /^-?[0-9]+$/;

    return er.test(value);
}

或者您也可以根据需要使用以下功能。它们由PHPJS Project开发

is_int() =>检查变量类型是否为整数,并且其内容是否为整数

is_float() =>检查变量类型是否为float,其内容是否为float

ctype_digit() =>检查变量类型是否为字符串,并且其内容是否只有十进制数字

更新1

现在它也检查负数,感谢@ChrisBartley评论

2020/03/11 12:30:22

尝试使用这些函数来测试一个值是否是没有小数部分且在可以表示为精确整数的大小限制内的数字原始值。

function isFloat(n) {
    return n === +n && n !== (n|0);
}

function isInteger(n) {
    return n === +n && n === (n|0);
}
JinJin古一2020/03/11 12:30:22

除以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年被标准化该解决方案涵盖在这个答案