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

后端 未结 4 908
轮回少年
轮回少年 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 10:45

    I know, its an old question, but why not just do:

    function onlyDecimals($number, $maxDecimalPlaces = 2) {
       return $amount == number_format($amount, $maxDecimalPlaces, ".", "");
    }
    

    See example and tests: http://sandbox.onlinephpfunctions.com/code/e68143a9ed0b6dfcad9ab294c44fa7e802c39dd0

提交回复
热议问题