php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73738 json_encode Digital precision problems
Submitted: 2016-12-14 14:07 UTC Modified: 2017-02-12 04:22 UTC
Votes:14
Avg. Score:4.4 ± 1.2
Reproduced:11 of 13 (84.6%)
Same Version:10 (90.9%)
Same OS:10 (90.9%)
From: chenjiablog at gmail dot com Assigned:
Status: No Feedback Package: json (PECL)
PHP Version: 7.1.0 OS: all
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2016-12-14 14:07 UTC] chenjiablog at gmail dot com
Description:
------------
When using this function to deal with an array containing decimal, there will be precision problems.

Test script:
---------------
<?php
$array['a'] = "as";
$array['b'] = 1;
$array['c'] = 1.13;
$array['d'] = 10.08;
echo json_encode($array);

Expected result:
----------------
{"a":"as","b":1,"c":1.13,"d":10.08}

Actual result:
--------------
{"a":"as","b":1,"c":1.1299999999999999,"d":10.08}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-01-30 16:42 UTC] jhdxr@php.net
-Status: Open +Status: Feedback
 [2017-01-30 16:42 UTC] jhdxr@php.net
this issue cannot be reproduced with your test script, see <https://3v4l.org/42HIs>
 [2017-02-01 06:03 UTC] sergey at lutskanu dot ru
jhdxr@php.net, I confirm that this problem exists and reproduced both in 7.1.0 and 7.1.1.
About your example I think it can be specific behaviour on 3v4l.org.

To reproduce issue you can try it from CLI for example:

# cat test.php
<?php
$array['a'] = "as";
$array['b'] = 1;
$array['c'] = 1.13;
$array['d'] = 10.08;
echo json_encode($array);

# php -v
PHP 7.1.1 (cli) (built: Jan 18 2017 11:37:34) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.1.1, Copyright (c) 1999-2017, by Zend Technologies

# php test.php
{"a":"as","b":1,"c":1.1299999999999999,"d":10.08}
 [2017-02-12 04:22 UTC] pecl-dev at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 20:01:31 2024 UTC