PHP: get number of decimal digits

前端 未结 18 2270
忘了有多久
忘了有多久 2020-11-28 09:25

Is there a straightforward way of determining the number of decimal places in a(n) integer/double value in PHP? (that is, without using explode)

18条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 10:11

    First I have found the location of the decimal using strpos function and increment the strpos postion value by 1 to skip the decimal place.

    Second I have subtracted the whole string length from the value I have got from the point1.

    Third I have used substr function to get all digits after the decimal.

    Fourth I have used the strlen function to get length of the string after the decimal place.

    This is the code that performs the steps described above:

         ";
            echo substr( $str, -(strlen($str)-(strpos($str, '.')+1)) );
            echo "
    "; echo strlen( substr( $str, -(strlen($str)-(strpos($str, '.')+1))) ); ?>

提交回复
热议问题