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

From: Date: Thu, 23 Nov 2017 15:17:21 +0000
Subject: Bug #75561 [Opn->Nab]: Json_encode does not encode numbers properly
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-212696@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:         requinix@php.net
 Reported by:        anantaramdas at outlook dot com
 Summary:            Json_encode does not encode numbers properly
-Status:             Open
+Status:             Not a bug
 Type:               Bug
-Package:            Reproducible crash
+Package:            JSON related
 Operating System:   Windows
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

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/


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