Home > php教程 > php手册 > PHP之浮点数计算比较以及取整数不准确的解决办法

PHP之浮点数计算比较以及取整数不准确的解决办法

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 19:50:09
Original
1202 people have browsed it

这篇文章主要介绍了PHP之浮点数计算比较以及取整数不准确的解决办法,代码超简单,需要的朋友可以参考下

php有意思的现象,,应该是很多编程语言都会有这样的现象。这个是因为计算机的本身对浮点数识别的问题.....下面通过代码给大家展示下:

$f = 0.58; var_dump(intval($f * 100 *100)); //结果5799 var_dump((float)($f * 100 *100)); //结果5800 echo (int)((0.1+0.7)*10); //结果7 echo (float)((0.1+0.7)*10); //结果8

再次需注意的是在浮点数计算的时候,切记不要把浮点数转换成整形,否则会出现不可能预估的错误。

以上代码就是PHP之浮点数计算比较以及取整数不准确的解决办法。

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template