php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75700 Incorrect number after json_encode
Submitted: 2017-12-17 18:30 UTC Modified: 2017-12-20 20:34 UTC
From: admin at twister-vl dot ru Assigned:
Status: Not a bug Package: JSON related
PHP Version: 7.2.0 OS: All
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: admin at twister-vl dot ru
New email:
PHP Version: OS:

 

 [2017-12-17 18:30 UTC] admin at twister-vl dot ru
Description:
------------
Some numbers after json_encode may contain unexpected zeros.

Test script:
---------------
<?php
$a =  (1010*(0.00003*100))/100;

echo $a;
echo "\n";
echo json_encode($a);

Expected result:
----------------
0.0303
0.0303

Actual result:
--------------
0.0303
0.030300000000000004

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-12-20 20:28 UTC] nikic@php.net
-Package: json +Package: JSON related
 [2017-12-20 20:34 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2017-12-20 20:34 UTC] nikic@php.net
The actual result is correct, please see https://3v4l.org/BfqFu.

If you'd like to receive the old result (though you should ask yourself why you want that) you can either
a) change the serialize_precision setting, understanding that you will no longer receive an accurate floating-point representation, or
b) explicitly round your floating point numbers to a lower level of precision.
 
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Fri Feb 03 00:03:49 2023 UTC