R
R
reds2020-05-04 17:29:35
PHP
reds, 2020-05-04 17:29:35

Why do fractional numbers become long after serialization?

PHP, I save an array to a file with this function:

function object2file($value, $filename)
{
$str_value = serialize($value);
$f = fopen($filename, 'w');
fwrite($f, $str_value);
fclose($f);
}

If you look at the file, then the number 8812.29 is stored as:

8812.2900000000008731149137020111083984375

Is it possible to somehow avoid to reduce the file size?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2020-05-04
@reds

Because the task of serialize is to transfer the value as accurately as possible.
8812.29 in binary is written as an infinite fraction 10001001101011.01(00101000111101011100). When converted back to decimal notation, such a tail of digits appears.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question