Edit report at https://bugs.php.net/bug.php?id=79908&edit=1
ID: 79908
Updated by: cmb@php.net
Reported by: stefan at HDmeteo dot com
Summary: json_decode decodes negative zero as positive zero
-Status: Open
+Status: Not a bug
Type: Bug
Package: JSON related
PHP Version: 7.4.8
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
If you want to preserve the floating point number (there are no
integers with value -0), you have to pass JSON_PRESERVE_ZERO_FRACTION;
see <https://3v4l.org/1ZRm8>.
Previous Comments:
------------------------------------------------------------------------
[2020-07-28 13:02:21] stefan at HDmeteo dot com
Description:
------------
While php_encode stores a negative zero float as "-0" and preserves its sign, the
subsequent php_decode loses the sign and decodes "-0" as "0".
Test script:
---------------
$array = [round(-0.1), round(0.1)]; // [-0,0]
print_r($array); // Array ( [0] => -0 [1] => 0 )
$json = json_encode($array, true); // "[-0,0]"
$array = json_decode($json); // [0,0]
print_r($array); // Array ( [0] => 0 [1] => 0 )
Expected result:
----------------
Array ( [0] => -0 [1] => 0 )
Actual result:
--------------
Array ( [0] => 0 [1] => 0 )
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79908&edit=1