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

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

    Try these functions to test whether a value is a number primitive value that has no fractional part and is within the size limits of what can be represented as an exact integer.

    function isFloat(n) {
        return n === +n && n !== (n|0);
    }
    
    function isInteger(n) {
        return n === +n && n === (n|0);
    }
    

提交回复
热议问题