dow66098 2012-10-16 13:48
浏览 28
已采纳

PHP圆形行为[关闭]

$r = 302 / 490;
printLn($r);
printLn(round($r, 2, PHP_ROUND_HALF_UP));

Gives

0.61632653061224484947189239392173476517200469970703125 0.61999999999999999555910790149937383830547332763671875

Why not normal rounding to really 2 digits?

  • 写回答

2条回答 默认 最新

  • drfu29983 2012-10-16 13:53
    关注

    To within the measurable precision of floating-point numbers, the rounded number shown is 0.62. To understand why you aren't getting exactly 0.62, you need to understand how floating-point numbers work. Basically you can take any integer of magnitude up to 253-1 and multiply it by any power of 2 between -1074 and 971 (inclusive). 0.62 cannot be exactly represented in this manner.

    Put another way, your precision INI setting is too high. See this codepad. Try setting it to a more representible number, like the default 15.

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

报告相同问题?

悬赏问题

  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计