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
 [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

Pull Requests

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: Mon Oct 07 15:01:28 2024 UTC