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

前端 未结 30 3063
栀梦
栀梦 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: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;
    }
    

提交回复
热议问题