php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73680 json_encode on window error
Submitted: 2016-12-08 01:56 UTC Modified: 2016-12-08 05:30 UTC
From: xltxlm at qq dot com Assigned:
Status: Not a bug Package: JSON related
PHP Version: 7.1.0 OS: Irrelevant
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:
MUST BE VALID
Solve the problem:
12 + 21 = ?
Subscribe to this entry?

 
 [2016-12-08 01:56 UTC] xltxlm at qq dot com
Description:
------------
var_dump(json_encode([100.1]));

Expected result:
----------------
string(7) "[100.1]"

Actual result:
--------------
string(20) "[100.09999999999999]"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-12-08 03:10 UTC] yohgaki@php.net
-Status: Open +Status: Not a bug -Operating System: window +Operating System: Irrelevant
 [2016-12-08 03:10 UTC] yohgaki@php.net
Use lower serialize_presision if you do not want this kind of outputs.

i.e. IEEE float value is not "precise" float value.
 [2016-12-08 05:30 UTC] yohgaki@php.net
The reason behind this change.

JSON is cross platform serialization and most systems uses IEEE 754 for float. Older PHP JSON truncated float value by "precision" ini setting which is less precise with regard to IEEE 754. i.e. Older PHP was losing precision by default.

PHP 7.1 uses "serialize_precision" which is the same precision for most systems. Therefore, PHP 7.1 or later JSON can handle float value more precisely than before.

If users want to use the value displayed by "echo $float_val;", set the same "prevision" ini value to "serialize_precision". Please note that this affects serialize() also.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 04:01:28 2024 UTC