I am generating 10 random floats between 6 and 8 (all for good reason), and writing them to a mysql database in a serialized form. But one quirk seems to emerge at the stora
Setting your serialize_precision value in php.ini to -1 will solve the floating point issue, or you can set it to a value that you prefer, as per the specifications here: http://php.net/manual/en/ini.core.php#ini.serialize-precision
PHP versions <= 5.3.5 shipped with the default value of "100", while the default now at version 7.0.33 is "17", although the package bundled with your distro might have shipped with a "-1"
As pointed out in other responses, you can override this setting in the application itself or even a custom php.ini that your VirtualHost container or .htaccess specifies.
I hope that helps :)
PHP.INI file contains a serialize_precision directive, which allows you to control how many significant digits will be serialized for your float. In your case, storing just one decimal of numbers between 6 to 8 means two significant digits.
You can set this setting in php.ini file or directly in your script:
ini_set('serialize_precision', 2);
If you do not care about the exact number of significant digits, but care about not having a spaghetti of digits resulting from the way float numbers are stored, you can also give a go to a value of -1, which invokes "special rounding algorithm", this is likely to do exactly what is required:
ini_set('serialize_precision', -1);
You can even reset it back to its original value after your serialization:
$prec = ini_get('serialize_precision');
ini_set('serialize_precision', -1);
... // your serialization here
ini_set('serialize_precision', $prec);
For me I found 3 ways:
preg_replace('/d:([0-9]+(\.[0-9]+)?([Ee][+-]?[0-9]+)?);/e', "'d:'.((float)$1).';'", $value);
where $value is your float; found hereIn any case I use variant #2