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

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

    You can use a simple regular expression:

    function isInt(value) {
    
        var er = /^-?[0-9]+$/;
    
        return er.test(value);
    }
    

    Or you can use the below functions too, according your needs. They are developed by the PHPJS Project.

    is_int() => Check if variable type is integer and if its content is integer

    is_float() => Check if variable type is float and if its content is float

    ctype_digit() => Check if variable type is string and if its content has only decimal digits

    Update 1

    Now it checks negative numbers too, thanks for @ChrisBartley comment!

提交回复
热议问题