php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70137 json_encode/decode should use max precision for float like values by default
Submitted: 2015-07-25 22:16 UTC Modified: 2016-09-06 17:52 UTC
From: yohgaki@php.net Assigned: bukka (profile)
Status: Closed Package: JSON related
PHP Version: Irrelevant OS: any
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: yohgaki@php.net
New email:
PHP Version: OS:

 

 [2015-07-25 22:16 UTC] yohgaki@php.net
Description:
------------
json_encode/decode uses 'precision'.
It should use 'serialize.precision' to handle float like JSON numeric to be more precise.


Test script:
---------------
[yohgaki@dev PHP-master]$  php -r '$v = 10/3; var_dump(json_encode($v));'
string(15) "3.3333333333333"

[yohgaki@dev PHP-master]$  php -r '$v = 10/3; ini_set("precision", 17);var_dump(json_encode($v));'
string(18) "3.3333333333333335"

[yohgaki@dev PHP-master]$ php
<?php
var_dump(json_decode('{ "v" : 0.12345678901234567890 }'));
ini_set('precision', 17);
var_dump(json_decode('{ "v" : 0.12345678901234567890 }'));
?>

object(stdClass)#1 (1) {
  ["v"]=>
  float(0.12345678901235)
}
object(stdClass)#1 (1) {
  ["v"]=>
  float(0.12345678901234568)
}



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-09-06 17:52 UTC] bukka@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: bukka
 [2016-09-06 17:52 UTC] bukka@php.net
This has been already done in precise float value RFC implementaion (sorry forgot to update this bug report) that is part of 7.1
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 13:01:29 2024 UTC