如何处理在PHP中浮动的奇怪的四舍五入 [英] How to deal with strange rounding of floats in PHP

查看:134
本文介绍了如何处理在PHP中浮动的奇怪的四舍五入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家知道,浮点运算并不总是完全准确的,但是你如何处理它的不一致?



例如,在PHP 5.2.9 : (这在5.3中不会发生)

  echo round (14.99225,4); // 14.9923 
echo round(15.99225,4); // 15.9923
echo round(16.99225,4); // 16.9922 ??
回合(17.99225,4); // 17.9922 ??
回合(25.99225,4); // 25.9922 ??
回合(26.99225,4); // 26.9923

如何解决这个问题?

解决方案

欢迎来到IEEE754,享受您的逗留< a>。



使用 bc gmp 来代替。


As we all know, floating point arithmetic is not always completely accurate, but how do you deal with its inconsistencies?

As an example, in PHP 5.2.9: (this doesn't happen in 5.3)

echo round(14.99225, 4);  // 14.9923 
echo round(15.99225, 4);  // 15.9923 
echo round(16.99225, 4);  // 16.9922 ??
echo round(17.99225, 4);  // 17.9922 ??
echo round(25.99225, 4);  // 25.9922 ??
echo round(26.99225, 4);  // 26.9923

How would you work around this?

解决方案

Welcome to IEEE754, enjoy your stay.

Use bc or gmp instead.

这篇关于如何处理在PHP中浮动的奇怪的四舍五入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆