PHP: How to check if a number has more than two decimals

后端 未结 4 899
轮回少年
轮回少年 2021-01-21 10:12

I\'m trying to pick out numbers with more than two decimals (more than two digits after the decimal separator). I cant\'t figure out why this doesn\'t work:

if (         


        
4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-21 11:04

    You could use a regex:

    $number = 1.12; //Don't match
    $number = 1.123; //Match
    $number = 1.1234; //Match
    $number = 1.123; //Match
    
    if (preg_match('/\.\d{3,}/', $number)) {
        # Successful match
    } else {
        # Match attempt failed
    }
    

提交回复
热议问题