1

I have an issue when trying to decode JSON with the json_decode function in PHP 7.0.21. I am able to replicate this problem with these lines of code:

Code:

<?php
$inputJSON = '{"value":0.00000883}';
$outputJSON = json_decode($inputJSON);
print_r($outputJSON);

Output:

stdClass Object
(
    [value] => 8.83E-6
)

I also tried changing the precision with ini_set('precision', 8); which doesn't change the output.

The only fixes I found online were regex replaces which changed the number into a string but that's a hack and a good solution. I don't want to change ALL my float numbers to strings.

Why is this happening and how can I fix this properly without adding a lot of overhead like using number_format. Is the parsing simply broken in json_decode?

8
  • 5
    That's the same number. 8.83E-6 is 8.83 * 10(^-6), which means go with the decimal point 6 times to the left. Commented Nov 27, 2017 at 11:29
  • 1
    What actual problem is this causing? Those formats are functionally identical in PHP. Commented Nov 27, 2017 at 11:29
  • 1
    That's called scientific notation. There are a bunch of dups out here, and here is one of them: Why is PHP printing my number in scientific notation, when I specified it as .000021? Commented Nov 27, 2017 at 11:30
  • 3
    That's just the result of casting a PHP float variable into string to be printed on screen (that's what print_r() accomplishes). If it's a problem in your DBMS, you must be using prepared statements incorrectly (or not using them at all). We need to see the relevant code. Commented Nov 27, 2017 at 11:37
  • 1
    If your column requires that format then number_format is hardly a overhead. Commented Nov 27, 2017 at 11:38

1 Answer 1

3

You can use number_format to remove the e-6 so you can store correctly in your database;

<?php

$inputJSON = '{"value":0.00000883}';
$outputJSON = json_decode($inputJSON);

$formatted = number_format($outputJSON->value,8);

print_r($formatted);

outputs: 0.00000883

Although, I'm pretty sure MySQL should handle 8.83E-6 as an input.

Sign up to request clarification or add additional context in comments.

4 Comments

This is indeed what fixes the display error on my part, I tried to look for an explanation at the wrong place while it was a database issue. Setting the ini_set('serialize_precision', -1); somehow fixed my database insertion problem.
In general terms this should just not be necessary. Not only do the major database engines I've tested (MySQL, SQL Server and Oracle) accept 8.83E-6 as number literal but whatever PHP database library you are using should handle numeric parameters just fine.
What would be the solutions if $inputJSON has multiple values to be formatted and I am returning an array $outputJSON = json_decode($inputJSON, true);
Hi @alex, you'll only want to run number_format on integers/longs/doubles and floats. So it would be $formatted = number_format($outputJSON['TheArrayKey'],8); where TheArrayKey is your array key for one of the above variable types. Since you have multiple to format, I would either make an object class for this or just loop through the results and reset the array. Check out how to pass by reference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.