dongming4994 2013-03-13 09:33
浏览 62
已采纳

unserialize + php给出转换原始值

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.

  • 写回答

1条回答 默认 最新

  • doushangxianq07480 2013-03-13 11:25
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?