Reputation: 108
I'm executing the following code in a PHP script:
$obj = new \stdClass();
$obj->lat = 0.000011399388312455;
echo json_encode($obj) . "\n";
The output of the script is
{"lat":1.1399388312454999446e-5}
As you can see, the number is represented in exponential notation. Is there some way to represent that particular number in extended notation, in the JSON-serialized object?
The desired output is
{"lat":0.000011399388312455}
The version of PHP is 5.6.30.
P.S.: This JSON-serialized object will be sent to an external web service that doesn't accept numbers in exponential form. Moreover, it needs to be a number and not a string ({"lat":"0.000011399388312455"}
won't work).
The number should not be rounded (it's a geographical coordinate).
Here are other similar questions, but they don't fit my case unfortunately.
php- floating point number shown in exponential form
Remove the "E" in a number format for very small numbers
Thank you in advance.
Upvotes: 3
Views: 567
Reputation: 131
It is implemented in PHP 7.1 https://wiki.php.net/rfc/precise_float_value
As a work around, use other JSON serializers not a default json_encode
.
E notation is valid form of float representation. In php >7.1 with precision set to -1 results of your code would look like: {"lat":1.1399388312455e-5}
which is absolutely the same as 0.000011399388312455
because you have 4 leading zeros.
If you still want to have leading zeroes in json i would go towards strings. They are perfectly fine for coordinates representations.
Upvotes: 3