Hi all,

Is there a function in PHP that will tell you how many decimal places a number (float) has? Thanks

    The PHP manual will tell all:

    The size of a float is platform-dependent, although a maximum of ~1.8e308 with a precision of roughly 14 decimal digits is a common value (that's 64 bit IEEE format).

    Of course, you could always create your own function for this:

    function precision($num)
    {
        $places = substr($num, strpos($num, '.')+1);
        return strlen($places);
    }

    Simple enough right? Get the spot where the decimal is, add 1 so you're not including the decimal, then just find the length of the string..... it could even be done on one line like:

    function precision($num) {
        return strlen(substr($num, strpos($num, '.')+1));
    }
      8 days later

      Thanks for the suggestion. I had thought about doing something like that, but thought: if there is a function that does it, why re-invent the wheel? I guess the answer is that there is no such in-built function.

      Thanks for the suggestion, I'll end up using that.

        Write a Reply...