PHP的整数舍入问题 [英] PHP integer rounding problems
本文介绍了PHP的整数舍入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么上面的输出7?我知道PHP是如何向0进行舍入的,但是不是以(0.1 + 0.7)* 10
作为浮点数计算,然后以整数形式进行转换? b
谢谢!
解决方案
当精度在内部转换为二进制等效值时,计算的值将是类似于7.9+而不是预期的8。
如果您需要高度的准确性,请使用 GMP 系列函数或者 bcmath 库。
echo (int) ( (0.1+0.7) * 10 );
Why does the above output 7? I understand how PHP rounds towards 0, but isn't (0.1+0.7) * 10
evaluated as a float and then casted as an integer?
Thanks!
解决方案
There's a loss in precision when decimals are converted internally to their binary equivalent. The computed value will be something like 7.9+ instead of the expected 8.
If you need a high degree of accuracy, use the GMP family of functions or the bcmath library.
这篇关于PHP的整数舍入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文