How do I check that a number is float or integer?

前端 未结 30 2567
栀梦
栀梦 2020-11-22 00:01

How to find that a number is float or integer?

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


        
相关标签:
30条回答
  • 2020-11-22 00:51

    It's simple as:

    if( n === parseInt(n) ) ...
    

    Try this in console:

    x=1;
    x===parseInt(x); // true
    x="1";
    x===parseInt(x); // false
    x=1.1;
    x===parseInt(x); // false, obviously
    
    // BUT!
    
    x=1.0;
    x===parseInt(x); // true, because 1.0 is NOT a float!
    

    This confuses a lot of people. Whenever something is .0, it's not a float anymore. It's an integer. Or you can just call it "a numeric thing" for there is no strict distinction like back then in C. Good old times.

    So basically, all you can do is check for integer accepting the fact that 1.000 is an integer.

    Interesting side note

    There was a comment about huge numbers. Huge numbers mean NO problem for this approach; whenever parseInt is unable to handle the number (for it's too big) it will return something else than the actual value so the test will return FALSE. This is a good thing because if you consider something a "number" you normally expect JS to be able to calculate with it - so yes, numbers are limited and parseInt will take this into consideration, to put it this way.

    Try this:

    <script>
    
    var a = 99999999999999999999;
    var b = 999999999999999999999; // just one more 9 will kill the show!
    var aIsInteger = (a===parseInt(a))?"a is ok":"a fails";
    var bIsInteger = (b===parseInt(b))?"b is ok":"b fails";
    alert(aIsInteger+"; "+bIsInteger);
    
    </script>
    

    In my browser (IE8) this returns "a is ok; b fails" which is exactly because of the huge number in b. The limit may vary but I guess 20 digits "ought to be enough for anybody", to quote a classical :)

    0 讨论(0)
  • 2020-11-22 00:52

    check for a remainder when dividing by 1:

    function isInt(n) {
       return n % 1 === 0;
    }
    

    If you don't know that the argument is a number you need two tests:

    function isInt(n){
        return Number(n) === n && n % 1 === 0;
    }
    
    function isFloat(n){
        return Number(n) === n && n % 1 !== 0;
    }
    

    Update 2019 5 years after this answer was written, a solution was standardized in ECMA Script 2015. That solution is covered in this answer.

    0 讨论(0)
  • 2020-11-22 00:52

    Some times Number objects don't allow you to use direct the mod operator (%), if you are facing that case you can use this solution.

    if(object instanceof Number ){
       if( ((Number) object).doubleValue() % 1 == 0 ){
          //your object is an integer
       }
       else{
          //your object is a double
       }
    }
    
    0 讨论(0)
  • 2020-11-22 00:52

    This maybe isn't as performant as the % answer, which prevents you from having to convert to a string first, but I haven't seen anyone post it yet, so here's another option that should work fine:

    function isInteger(num) {
        return num.toString().indexOf('.') === -1;
    }
    
    0 讨论(0)
  • 2020-11-22 00:53

    Try these functions to test whether a value is a number primitive value that has no fractional part and is within the size limits of what can be represented as an exact integer.

    function isFloat(n) {
        return n === +n && n !== (n|0);
    }
    
    function isInteger(n) {
        return n === +n && n === (n|0);
    }
    
    0 讨论(0)
  • 2020-11-22 00:53

    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

    0 讨论(0)
提交回复
热议问题