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

前端 未结 30 2628
栀梦
栀梦 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:47

    Here's my code. It checks to make sure it's not an empty string (which will otherwise pass) and then converts it to numeric format. Now, depending on whether you want '1.1' to be equal to 1.1, this may or may not be what you're looking for.

    var isFloat = function(n) {
        n = n.length > 0 ? Number(n) : false;
        return (n === parseFloat(n));
    };
    var isInteger = function(n) {
        n = n.length > 0 ? Number(n) : false;
        return (n === parseInt(n));
    };
    
    var isNumeric = function(n){
    
       if(isInteger(n) || isFloat(n)){
            return true;
       }
       return false;
    
    };
    

提交回复
热议问题