php.net |  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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
29 - 10 = ?
Subscribe to this entry?

 
 [2020-12-04 10:25 UTC] 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}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-12-04 12:31 UTC] cmb@php.net
-Type: Bug +Type: Feature/Change Request -Package: *General Issues +Package: JSON related
 [2020-12-04 12:31 UTC] 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 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
https://3v4l.org/LlIqF
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 06:01:32 2024 UTC