Rajat Modi
Rajat Modi

Reputation: 1343

unseralize + php gives convert original value

Here is my string I want unserialize this string.

$string3 = 'a:3:{i:0;a:2:{s:5:"value";d:4.0999999999999996;s:7:"players";a:2:{i:6;i:6;i:7;i:7;}}i:1;a:2:{s:5:"value";d:10.899999999999999;s:7:"players";a:1:{i:7;i:7;}}i:2;a:2:{s:5:"value";d:1.7763568394002505E-15;s:7:"players";N;}}';

and it gives below result

Array
(
[0] => Array
    (
        [value] => 4.1 // this value converted to 4.1 original was see in the string it was 4.0999999999999996
        [players] => Array
            (
                [6] => 6
                [7] => 7
            )

    )

[1] => Array
    (
        [value] => 10.9 // this value converted to 10.9 original was see in the string it was 10.899999999999999
        [players] => Array
            (
                [7] => 7
            )

    )

[2] => Array
    (
        [value] => 1.7763568394003E-15
        [players] => 
    )

)

it converts array value to 4.1 and 10.9 etc. I want original value not converted.

Upvotes: 1

Views: 128

Answers (1)

w00
w00

Reputation: 26762

It is hard for computers to handle floats with precision. Without going into much details PHP simply has set a value for the max amount of floating point values. This is set in the precision config property. It is probably set to 14, so every float that has 14 or more floating numbers will be rounded.

So what you need to do is increase the precision value in your php.ini. Or change it with ini_set('precision', 20).

That should do the trick.

Upvotes: 1

Related Questions