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

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

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

提交回复
热议问题