How to check if a number is float or integer?-Collection of common programming errors

how to find if a number is float or integer?

1.25 --> float  
1 --> integer  
0 --> integer  
0.25 --> float
  1. check a remainder when dividing by 1:

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

    If you don’t know that the argument is a number-

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

    If you also want to include examples such as 1E308 is a float, and not an integer:

    function isInt(n) {
       return typeof n === 'number' && parseFloat(n) == parseInt(n, 10) && !isNaN(n);
    } // 6 characters
    
  2. Try this.

    function isFloat (n) {
      return n===+n && n!==(n|0);
    }
    
    function isInteger (n) {
      return n===+n && n===(n|0);
    }
    
  3. Here are efficient functions that check if the value is a number or can be safely converted to a number:

    function isNumber(value) {
        if ((undefined === value) || (null === value)) {
            return false;
        }
        if (typeof value == 'number') {
            return true;
        }
        return !isNaN(value - 0);
    }
    

    And for integers (would return false if the value is a float):

    function isInteger(value) {
        if ((undefined === value) || (null === value)) {
            return false;
        }
        return value % 1 == 0;
    }
    

    The efficiency here is that parseInt (or parseNumber) are avoided when the value already is a number. Both parsing functions always convert to string first and then attempt to parse that string, which would be a waste if the value already is a number.

    Thank you to the other posts here for providing further ideas for optimization!

  4. You can use a simple regular expression:

    function isInt(value)
    {
        var er = /^[0-9]+$/;
    
        return ( er.test(value) ) ? true : false;
    }
    

    Or you can use the below functions too, according your needs. They are developed by the PHPJS Project.

    is_int() => Check if the variable type is Integer and if its content is Integer

    is_float() => Check if the variable type is Float and if its content is Integer

    ctype_digit() => Check if the variable type is String and if its content has only decimal digits

  5. Why not something like this:

    var isInt = function(n) { return parseInt(n) === n };
    
  6. 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

  7. 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); }
    
  8. THIS IS FINAL CODE FOR CHECK BOTH INT AND FLOAT

    function isInt(n) { 
       if(typeof n == 'number' && Math.Round(n) % 1 == 0) {
           return true;
       } else {
           return false;
       }
    } 
    

    OR

    function isInt(n) {   
       return typeof n == 'number' && Math.Round(n) % 1 == 0;   
    }   
    
  9. 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');
    }
    
  10. function isInt(n) 
    {
        return n != "" && !isNaN(n) && Math.round(n) == n;
    }
    function isFloat(n){
        return n != "" && !isNaN(n) && Math.round(n) != n;
    }
    

    works for all cases.

  11. Another method is:

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

    Might not be as efficient as the others but another method all the same.

  12. !!(24%1) // false
    !!(24.2%1) // true
    
  13. 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;
    }
    
  14. It really doesn’t have to be so complicated. The numeric value of an integer’s parseFloat() and parseInt() equivalents will be the same. Thus you can do like so:

    function isInt(value){ 
        return (parseFloat(value) == parseInt(value)) && !isNaN(value);
    }
    

    Then

    if (isInt(x)) // do work
    

    This will also allow for string checks and thus is not strict. If want a strong type solution (aka, wont work with strings):

    function is_int(value){ return !isNaN(parseInt(value * 1) }
    
  15. 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.

  16. float values consists of ., so here we can use indexOf method.

    function isInt(number){
        if(number.toString().indexOf(".") > -1)
            return false; // it is float
        else 
            return true; //it is integer
    }  
    

    The above code is only applicable if the given variable is number. if you are not sure about the type of variable , then do the following before the above code:

    //as suggested by @kennebec
    function isNumber(number){
        return typeof n === 'number'
    }
    
  17. 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.

  18. Here’s what I use for integers:

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

    Short, nice 🙂 Works all the time. This is what David Flanagan suggests if I’m not mistaken.

  19. parseInt(yourNumber)=== parseFloat(yourNumber)
    

Originally posted 2013-11-09 21:01:54.