drf21989 2016-02-09 09:53
浏览 134
已采纳

PHP中科学记数法的数字不正确

I'm reading data from spreadsheets that contain long numeric ID strings. I'm trying to make my system foolproof by making sure that the data reads correctly even if the column format is incorrectly set as a number.

So the spreadsheet contains the number 32486509223273700000 in scientific notation as 3.2486509223274E+19. Before i store it as a string i need to convert it to the original value. This is where the problems come in.

I can't make PHP convert the scientific notation back to the original number. Using various methods for converting the number back to original, it always seems to have different digits instead of trailing zeros.

$i = 3.2486509223274E+19;
echo $i; //32486509223274000384
echo number_format($i,0,'',''); //32486509223274000384
echo sprintf('%0.0f', $i); //32486509223274000384

Please note that I intend to store the end result as a string.

  • 写回答

3条回答 默认 最新

  • dongshan1811 2016-02-09 10:27
    关注

    Note that the double floating point format has a precision of about 15.5 decimals. All other displayed decimals are extrapolated. And as always, extrapolation is a dangerous game.

    Or in other words, 3.2486509223274E+19 represents an interval, roughly from 3.24865092232739984E+19 to 3.24865092232740024E+19. Any integer in that interval has to count as "correct" extrapolation.

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

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?