intval碰到小数为什么会减1

WBOY
Release: 2016-06-13 11:57:21
Original
1105 people have browsed it

intval遇到小数为什么会减1
$a = 2.3;
echo ($a*100);//输出230
echo intval($a*100);//输出229
刚开始用的时候我都凌乱了,不知道这是怎么回事
查了好多资料才找到答案,如下
我们看到的:2.3在机器中实际是这样的:2.299999999...具体为什么我也不知道
所以2.3*100在机器中实际是2.299999..*100=229.99...
然后intval作用于他,省略了小数部分,于是就成了229,而不是230
但是intval(1.0)仍然是1,原因是1.0在机器中也是1.0,而不是0.99999...
这种机制应该只会在小数点后发挥作用吧

求大神从根源解释下这个神奇的问题
------解决方案--------------------
printf('%.16f', 2.3); //2.2999999999999998

你在学习计算机基础知识时,没有看二进制小数的章节吗?

Related labels:
source:php.cn
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!