我将一些会计脚本升级到 PHP 8.1,但是我得到的 float 不正确。
我循环了一些交易借方/贷方并且余额为 0 但是最后当我尝试比较零时它不适用于 == 0 和 === 0
经过几次调试我发现:
<?php
var_dump(1097.5 - 835.7);
返回 float (261.79999999999995)
PHP 7 中的同一行返回 float (261.8)
这可以很容易地使用这个 sandbox 进行测试
所以我想知道是否可以在我的 PHP 配置中更改任何设置,以确保我获得与之前相同的 float 结果,而不必在数百个脚本的每个阶段都进行四舍五入?
最佳答案
PHP 8.0 UPGRADE NOTES :
var_dump() and debug_zval_dump() will now print floating-point numbers using serialize_precision rather than precision. In a default configuration, this means that floating-point numbers are now printed with full accuracy by these debugging functions.
所以你可以改变这个
ini_set('serialize_precision', 16);
https://3v4l.org/uOAPD#v8.1rc3
但是,我怀疑这是您的真正问题!因为此更改仅影响“这些调试功能”以及 serialize
、json_encode
关于floating-point - PHP 8 浮点小数点不同于 PHP 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69858549/