Search code examples
phpfloating-accuracy

PHP number more than 10 decimal places


I have this situation:

$a = (double)"8.876543456787654";
echo json_encode(["value" => $a]);

It's returning this:

{"value":8.8765434567877}

And not this, which is the desirable:

{"value":8.876543456787654}

Note that I can't have this:

{"value":"8.876543456787654"}

How can I change this precision and cancel the rounding?


Solution

  • Set precision higher at runtime

    ini_set("precision", "16");

    Also use (float) instead of (double)