简体   繁体   English

PHP。 round()工作很奇怪

[英]PHP. round() working strange

Here is the code: 这是代码:

$val = 65.5401000000000001;
return round($val ,2);

I'm expecting to get 65.54, but getting 65.54000000000001. 我期望得到65.54,但得到65.54000000000001。 Why it works this way? 为什么这样工作? And how to get an expected value? 以及如何获得期望值?

The value of the precision setting in your PHP.ini file is too high. PHP.ini文件中的precision设置值太高。 It's best to keep it at the default 15 to prevent this happening. 最好将其保留为默认值15,以防止发生这种情况。 Basically what you're seeing is the consequence of floating point numbers not being able to handle real numbers exactly. 基本上,您所看到的是浮点数无法精确处理实数的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM