Home > Backend Development > PHP Tutorial > Parsing unexpected exceptions when converting int such as intval() in PHP_PHP Tutorial

Parsing unexpected exceptions when converting int such as intval() in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:04:20
Original
1261 people have browsed it

First take a look at the following test code on the Internet:

Copy the code The code is as follows:

$a = 9.45*100;
var_dump($a);
var_dump(intval($a));
$a = 945*1.00;
var_dump($a);
var_dump(intval($a));
?>

Operating results: float(945) int(944) float(945) int(945)
Although this code tells the results, many people still If you don't understand it, you can't explain why unexpected transformations happen.
Everything on the Internet about this situation is vague and incomprehensible. Let me briefly explain here:
The number 9.45 looks like this when we see it, but it is not this inside the machine, but 9.449999999999999999.... So:
9.449999*100 = 944.9999. So you can understand it, right? intval directly removes the mantissa, this one is called Shenma Lai, haha, I forgot the name. In this way, intval is almost the same as the floor() function. hehe. This is something I didn't notice before. Didn't notice that intval will be rounded down.
There is no such thing as 1.0099999 in 1.00, so 945*1.00 will have a float of 945. Then the intval will naturally not be 944 when it is transformed.
There are also some classic exam questions, such as: intval((0.1+0.7)*10) is equal to 7 instead of 8. This is all true. alright. Just say it simply. At least it's much better than a lot of incomprehensible stuff on the Internet.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327772.htmlTechArticleFirst look at a test code on the Internet: Copy the code as follows: ?php $a = 9.45*100 ; var_dump($a); var_dump(intval($a)); $a = 945*1.00; var_dump($a); var_dump(intval($a))...
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