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

From: Date: Thu, 09 Oct 2014 17:36:44 +0000
Subject: Bug #68200 [Opn->Nab]: json_decode rounding floats to 3 digits precision
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-187989@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:             Open
+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:

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.


Previous Comments:
------------------------------------------------------------------------
[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 (#187989) next »