Mr. Felix Mr. Felix - 4 months ago 22
PHP Question

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?

Answer

Set precision higher at runtime

ini_set("precision", "16");

Also use (float) instead of (double)