KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
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 storage time: Before storing I'm just outputting the same data to see what it looks like, and this is the result I get a:10:{i:0;d:6.20000000000000017763568394002504646778106689453125;i:1;d:7.5999999999999996447286321199499070644378662109375;i:2;d:6.4000000000000003552713678800500929355621337890625;..} As you can see, I'm getting long numbers like 6.20000000000000017763568394002504646778106689453125 instead of what I'd really to like see, just 6.2. This is happening only when I serialize the data, if I just output the array, I do get the floats to one decimal. Here is my code: function random_float ($min,$max) { return ($min+lcg_value()*(abs($max-$min))); } $a1 = random_float(6, 8); $a1 = round($a1, 1); $a2 = random_float(6, 8); $a2 = round($a2, 1); $a3 = random_float(6, 8); $a3 = round($a3, 1); ... $array = array($a1, $a2, $a3, $a4, $a5, $a6, $a7, $a8, $a9, $a10); echo serialize($array);
Tags (comma-separated)
Save Edits
Cancel