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
)
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))) );
?>