|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #80484 json_encode() without converting string to float
Submitted: 2020-12-04 10:25 UTC Modified: 2020-12-04 12:31 UTC
From: teflontelefon at gmail dot com Assigned:
Status: Open Package: JSON related
PHP Version: Irrelevant OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: teflontelefon at gmail dot com
New email:
PHP Version: OS:


 [2020-12-04 10:25 UTC] teflontelefon at gmail dot com
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:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2020-12-04 12:31 UTC]
-Type: Bug +Type: Feature/Change Request -Package: *General Issues +Package: JSON related
 [2020-12-04 12:31 UTC]
I don't see how this behavior would be a bug.  You explicitly
specified the JSON_NUMERIC_CHECK flag, and the result is valid
 [2020-12-04 14:10 UTC] antonino dot spampinato86 at gmail dot com
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
 [2020-12-04 20:32 UTC] antonino dot spampinato86 at gmail dot com
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Jul 16 15:01:28 2024 UTC