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

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

    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;   
    }   
    

提交回复
热议问题