php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76934 json encode bug php 7.1 and above
Submitted: 2018-09-26 08:53 UTC Modified: 2018-09-26 09:36 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: grzesiek dot kesik at gmail dot com Assigned:
Status: Not a bug Package: JSON related
PHP Version: 7.1.22 OS: any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: grzesiek dot kesik at gmail dot com
New email:
PHP Version: OS:

 

 [2018-09-26 08:53 UTC] grzesiek dot kesik at gmail dot com
Description:
------------
After some calculation json_encode encounter problem with casting numbers

Test script:
---------------
<?php

$n1 = 9.79;
$n2 = $n1 *100;

echo $n1;
echo '  ';
echo $n2;

echo ' ';

echo json_encode($n2);

echo ' ';
$n3 = 979;
echo json_encode($n3);

Expected result:
----------------
echo json_encode($n2);

should be equals 979

Actual result:
--------------
echo json_encode($n2);

is equal 978.9999999999999 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-09-26 08:55 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2018-09-26 08:55 UTC] requinix@php.net
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

http://php.net/manual/en/function.json-encode.php
> Changelog
> 7.1.0  serialize_precision is used instead of precision when encoding double values.

http://php.net/manual/en/ini.core.php#ini.serialize-precision

Check your serialize_precision setting.
 [2018-09-26 09:30 UTC] grzesiek dot kesik at gmail dot com
According to requinix@php.net comment, I have changed serialize_precision setting to 17 (as in php 7.0 http://php.net/manual/en/ini.list.php). For me it does not work. After restarting apache i still gets this rounding issue.
 [2018-09-26 09:36 UTC] nikic@php.net
You will have to change it to a lower value such as 14 to restore the previous behavior.

However, you should seriously consider if that's really what you want -- you are actively corrupting your JSON output by reducing the precision with which floating point numbers are exported. Are you sure that is what you want to do? Most likely it is not.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu Dec 05 17:01:24 2019 UTC