Replies: 3 comments 1 reply
-
Your database float is only 4 bytes in length and your PHP float is 8 bytes in length. Create a double column, you'll get a "correct" value. But both types of real numbers are just an approximations. More details on wiki IEEE_754. |
Beta Was this translation helpful? Give feedback.
-
I checked the database, and the column is defined as |
Beta Was this translation helpful? Give feedback.
-
I used |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have the float
-30.22058000
stored in the database and accessed via a model. When I return an array of those models in JSON, the float is turned into-30.220579999999998
. In aJsonResource
, I hardcoded the value to the original-30.22058000
instead of returning the property of the model. The response now contains-30.22058
. If I dump the property viadump($this->myFloat)
orvar_dump($this->myFloat)
, I see the float-30.22058
. I'm trying to figure out what causes the difference in the JSON encoded response between when I hardcode the float value vs pull it from the database. Is it possible that the value in the property is not really what is being dumped, and dump is showing an approximation? There are no casts configured for this model. Myserialize_precision
is set to-1
in myphp.ini
file. I've tried to trace this through the JSON serialization and I can also see the float value is still as expected at the endJsonResource::resolve
. This would lead me to believe the same value is going into the actualjson_encode
function. Any insight on this would be great.My solution for now, will be in the resource to return
floatval(number_format($this->myFloat, 8))
. This seems weird since I'm still returning a float, but the value for this is the shorter form of-30.22058
.Beta Was this translation helpful? Give feedback.
All reactions