Bug #75561 [Nab]: Json_encode does not encode numbers properly

From: Date: Thu, 23 Nov 2017 15:28:58 +0000
Subject: Bug #75561 [Nab]: Json_encode does not encode numbers properly
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-212697@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=75561&edit=1

 ID:                 75561
 Updated by:         nikic@php.net
 Reported by:        anantaramdas at outlook dot com
 Summary:            Json_encode does not encode numbers properly
 Status:             Not a bug
 Type:               Bug
 Package:            JSON related
 Operating System:   Windows
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

More specifically, you want to leave serialize_precision at the default value of -1. Not that your
sample code could result in this problem in any case, but I assume that your 26000 is really the
result of a floating-point computation those value is close to 26000.0.


Previous Comments:
------------------------------------------------------------------------
[2017-11-23 15:17:20] requinix@php.net

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php

http://php.net/manual/en/function.json-encode.php
> additionally the encoding of float values depends on the value of serialize_precision.

Floating point values have a limited precision. Hence a value might 
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly 
printing it without any mathematical operations.

If you would like to know more about "floats" and what IEEE
754 is, read this:
http://www.floating-point-gui.de/

------------------------------------------------------------------------
[2017-11-23 12:40:24] anantaramdas at outlook dot com

Description:
------------
I have PHP version 7.1.7






Test script:
---------------
$amount = 26000;
\Log::info("A: " . $amount);
$json = json_encode([
    'amount' => $amount,
]);
\Log::info("J: " . $json);

Expected result:
----------------
Expected Behavior
Gives following output:
info log [13:26:42] LOG.info: A: 26000
info log [13:26:42] LOG.info: J: {"amount":26000}

Actual result:
--------------
Gives following output:
info log [13:26:42] LOG.info: A: 26000
info log [13:26:42] LOG.info: J: {"amount":25999.999999999996}




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



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


Thread (3 messages)

« previous php.bugs (#212697) next »