Bug #68200 [Nab]: json_decode rounding floats to 3 digits precision

From: Date: Fri, 10 Oct 2014 18:43:03 +0000
Subject: Bug #68200 [Nab]: json_decode rounding floats to 3 digits precision
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-188009@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=68200&edit=1

 ID:                 68200
 Updated by:         requinix@php.net
 Reported by:        vwirus at o2 dot pl
 Summary:            json_decode rounding floats to 3 digits precision
 Status:             Not a bug
 Type:               Bug
 Package:            JSON related
 Operating System:   linux/debian
 PHP Version:        5.5.17
 Block user comment: N
 Private report:     N

 New Comment:

> But it's not very stright forward and not possible in  shared hosting.
All you need to add is
  ini_set("precision", 15);


Previous Comments:
------------------------------------------------------------------------
[2014-10-10 09:20:00] vwirus at o2 dot pl

Thank you that solved my problem. But it's not very stright forward and not possible in  shared
hosting. A s json is "transport" layer for most php apps I think there should be flag in
json_decode similar to JSON_BIGINT_AS_STRING f.e. JSON_FLOAT_AS_STRING that converts floats to
stings and avoid rounding and losing precision. Such string could be easy used with BC_MATH

------------------------------------------------------------------------
[2014-10-09 17:36:44] requinix@php.net

14 digits of precision goes all the way to
  1000000.66599998
                ^
so yes, it will round to .666. By the way, "significant digits" are not just the decimal
places so it rounded to 10 digits, not 3.

Push your precision up to 15 or more and it will save as you want.

------------------------------------------------------------------------
[2014-10-09 14:22:21] marcin at urzenia dot net

It's float problem, not JSON:

var_dump((float)"1000000.66599998") => float(1000000.666)

------------------------------------------------------------------------
[2014-10-09 14:00:18] vwirus at o2 dot pl

Description:
------------
json_decode rounding floats to 3 digits precision. My precision setting in php.ini shows 14.


Test script:
---------------
$tmp = json_decode('{"number":1000000.66599998}', true);
        if (false !== strpos((string) $tmp['number'], '1000000.666')) {
            echo PHP_EOL, 'WRONG', PHP_EOL;
        }
        else {
            echo PHP_EOL, 'OK', PHP_EOL;
        }
        var_dump($tmp, $tmp['number']);

Expected result:
----------------
After decode the value of $tmp['number'] should be 1000000.66599998.

Actual result:
--------------
After decode the value of $tmp['number'] shows be 1000000.666 (it rounds real value with 3
digits precision)


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=68200&edit=1


Thread (8 messages)

« previous php.bugs (#188009) next »