dth96108 2018-10-10 08:11
浏览 158
已采纳

当我使用json_encode时为什么PHP会绕过我的号码?

I need execute json_encode() and convert my original number from:

50610101800060384093800100001010000000056199999999

to

"50610101800060384093800100001010000000056199999999" 

But it return

5.061010180006E+49

I tried this:

ini_set('precision', 30); //With 1, 30, 50, 100, 1000
ini_set('serialize_precision', -1);
'content' => json_encode($params, JSON_NUMERIC_CHECK)

but doesn't work. Can you help me?

  • 写回答

2条回答 默认 最新

  • duanmengmiezen8855 2018-10-10 10:15
    关注

    50610101800060384093800100001010000000056199999999 exceeds the value of the maximum integer in PHP and so it is promoted to a float and expressed in scientific notation. The float result may be problematic for various reasons as the Manual explains in warning about floating point precision.

    If you wish to express the value as if it were an integer you must encapsulate it in a string. That string you may add zero to it but when you do so the result in scientific notation will refer to a float, as follows:

    <?php
    
    $s = "50610101800060384093800100001010000000056199999999";
    
    echo $s,"
    ";
    
    $x = $s + 0;
    
    echo $x, "
    ",is_float($x); 
    

    See here.

    For more info in re PHP and floats, see here.

    On the other hand, if there were an array of numbers whose digits corresponded to the numerical display in the OP's post, you could write code as follows:

    <?php
        $a = [5,0,6,1,0,1,0,1,8,0,0,0,6,0,3,8,4,0,9,3,8,0,0,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,5,6,1,9,9,9,9,9,9,9,9];
        foreach($a as $e) {
           $e = (string) $e;
        }
        $foo = join($a);
        var_dump($foo);
        $foo = bcadd($foo, 1);
        var_dump($foo);
    

    See live code.

    The reason this example works is because each array value is converted to a numerical string and then the individual elements are joined to form one very long numerical string. BC Math is an extension in PHP which supports arbitrary precision. In this case, the bcadd() function adds one to the numerical string which results in the display of an incremented numerical string value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能