|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #72567 PHP 7.1 changes in json_encode() wrt. encoding floats undocumented
Submitted: 2016-07-09 10:34 UTC Modified: 2016-07-10 09:30 UTC
From: sjon at hortensius dot net Assigned: cmb (profile)
Status: Closed Package: JSON related
PHP Version: PHP 7.1 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
41 - 16 = ?
Subscribe to this entry?

 [2016-07-09 10:34 UTC] sjon at hortensius dot net
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

Using JSON_NUMERIC_CHECK has no influence as can be seen on the original

Test script:
echo json_encode(array(0.1, 0.2, 0.3, 0.4, 0.5, 0.6,));

Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-07-09 11:58 UTC]
-Summary: json_encode results in rounding errors when encoding floats +Summary: PHP 7.1 changes in json_encode() wrt. encoding floats undocumented -Status: Open +Status: Verified -Type: Bug +Type: Documentation Problem -PHP Version: Next Major Version +PHP Version: PHP 7.1 -Assigned To: +Assigned To: cmb
 [2016-07-09 11:58 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 <>.

[1] <>
 [2016-07-09 12:20 UTC]
Automatic comment from SVN on behalf of cmb
Log: Fix #72567: PHP 7.1 changes in json_encode() wrt. encoding floats undocumented
 [2016-07-09 12:21 UTC]
-Status: Verified +Status: Closed
 [2016-07-09 12:21 UTC]
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.
 [2016-07-09 15:09 UTC]
Instead of changing serialize_precision to 14, it is recommended to leave it at the new default value of -1.
 [2016-07-10 09:30 UTC] sjon at hortensius dot net
Thanks for the hints. I previously configured serialize_precision=24 (on to iron out differences between versions but I'll remove that; making it behave better in 7.1.

It seems to be pretty stable for non-eol versions anyway
 [2020-02-07 06:07 UTC]
Automatic comment on behalf of cmb
Log: Fix #72567: PHP 7.1 changes in json_encode() wrt. encoding floats undocumented
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 27 17:01:34 2024 UTC