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

前端 未结 30 2634
栀梦
栀梦 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条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 00:38

    function int(a) {
      return a - a === 0 && a.toString(32).indexOf('.') === -1
    }
    
    function float(a) {
      return a - a === 0 && a.toString(32).indexOf('.') !== -1
    }
    

    You can add typeof a === 'number' if you want to exclude strings.

提交回复
热议问题