|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2016-07-09 10:34 UTC] sjon at hortensius dot net
Description: ------------ Not sure what the correct terminology is for this; but it seems using json_encode with floats will result in 'rounding errors' as illustrated by the script below. This happens since alpha3 as can be seen on https://3v4l.org/q3SU6 Using JSON_NUMERIC_CHECK has no influence as can be seen on the original https://3v4l.org/TvGbe Test script: --------------- <?php echo json_encode(array(0.1, 0.2, 0.3, 0.4, 0.5, 0.6,)); Expected result: ---------------- [0.1,0.2,0.3,0.4,0.5,0.6] Actual result: -------------- [0.100000000000000005551115,0.20000000000000001110223,0.29999999999999998889777,0.40000000000000002220446,0.5,0.59999999999999997779554] PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 16:00:01 2025 UTC |
json_encode() now uses the ini option serialize_precision instead of precision when encoding double values. That behavioral change was a deliberate decision proposed and voted upon in RFC "More precise float value handling"[1]. Use `ini_set('serialize_precision', 14)` to restore the old behavior, see <https://3v4l.org/3ZuTQ>. [1] <https://wiki.php.net/rfc/precise_float_value>