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

JavaScript

JinJin老丝

2020-03-11

如何找到一个数字是floatinteger

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

第679篇《如何检查数字是浮点数还是整数?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

14个回答
古一古一 2020.03.11

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小哥Tom 2020.03.11

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

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

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;
}
达蒙JinJin 2020.03.11
!!(24%1) // false
!!(24.2%1) // true
小胖逆天 2020.03.11

How about this one?

isFloat(num) {
    return typeof num === "number" && !Number.isInteger(num);
}
路易猴子Pro 2020.03.11

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

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.

小卤蛋Tony 2020.03.11

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); }
TonyDavaidLEY 2020.03.11
function isInt(n) 
{
    return n != "" && !isNaN(n) && Math.round(n) == n;
}
function isFloat(n){
    return n != "" && !isNaN(n) && Math.round(n) != n;
}

适用于所有情况。

StafanL 2020.03.11

为什么不这样:

var isInt = function(n) { return parseInt(n) === n };
猿老丝 2020.03.11

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

function isInt(value) {

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

    return er.test(value);
}

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

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

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

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

更新1

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

null 2020.03.11

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

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

function isInteger(n) {
    return n === +n && n === (n|0);
}
JinJin古一 2020.03.11

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

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android