Req #80484 [Com]: json_encode() without converting string to float
| From: | antonino dot spampinato86 at gmail dot com | Date: | Fri, 04 Dec 2020 14:10:47 +0000 |
| Subject: | Req #80484 [Com]: json_encode() without converting string to float | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-230846@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=80484&edit=1
ID: 80484
Comment by: antonino dot spampinato86 at gmail dot com
Reported by: teflontelefon at gmail dot com
Summary: json_encode() without converting string to float
Status: Open
Type: Feature/Change Request
Package: JSON related
Operating System: Linux
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
json numeric check checks if it is a numeric sequence, php for double or float uses scientific
notation when they are too small or too large. Scientific notation was created for human readable
reading but in some circumstances you may have 16 or 14 digits before the exponent E. And if you
want to use values ââoutside this context use math libraries not native php computation.
This 1.0e-8 is 0.00000001
Previous Comments:
------------------------------------------------------------------------
[2020-12-04 12:31:03] cmb@php.net
I don't see how this behavior would be a bug. You explicitly
specified the JSON_NUMERIC_CHECK flag, and the result is valid
JSON.
------------------------------------------------------------------------
[2020-12-04 10:25:58] teflontelefon at gmail dot com
Description:
------------
json_encode() with JSON_NUMERIC_CHECK needs a way to encode strings as numbers WITHOUT converting
them to float first. You just can't use money types and pass them with the built in
json_encode() feature. I've looked all over and there is no solution except to create my own
json_encode() which will bear performance issues compared to the built-in method.
Test script:
---------------
echo json_encode(['x'=>'0.00000001'], JSON_NUMERIC_CHECK)."\n";
Expected result:
----------------
{"x":0.00000001}
Actual result:
--------------
{"x":1.0e-8}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80484&edit=1