serialization - PHP serialize() output for number is whack -
i calculating size of file upload in php, format in mb 1 decimal place so:
$metadata['upload_data'] = intval($_files['filedata']['size'] / 104857.6) / 10;
when echo $metadata['upload_data']
output 1.7
expect. when serialize array serialize($metadata)
, save file, output is:
a:2:{s:7:"uploads";i:11;s:11:"upload_data";d:1.6999999999999999555910790149937383830547332763671875;}
i'm trying efficient storing file sizes in mb not bytes, seems worse! why php store way? , going right way? thanks
from manual: http://php.net/manual/en/language.types.float.php
additionally, rational numbers representable floating point numbers in base 10, 0.1 or 0.7, not have exact representation floating point numbers in base 2, used internally, no matter size of mantissa. hence, cannot converted internal binary counterparts without small loss of precision. can lead confusing results: example, floor((0.1+0.7)*10) return 7 instead of expected 8, since internal representation 7.9999999999999991118....
i suggest using json_encode
, json_decode
if want see "1.7" in serialized version of array. these functions end being quicker serialize
, unserialize
, easier read (by easier read, mean person reading them, rather machine).
Comments
Post a Comment