Parsing unexpected exceptions during int conversion such as intval () in PHP _ PHP Tutorial

Source: Internet
Author: User
Parse unexpected exceptions in int conversions such as intval () in PHP. Let's take a look at the following online test code: Copy the code as follows :? Php $ a9.45 * 100; var_dump ($ a); var_dump (intval ($ a); $ a945 * 1.00; var_dump ($ a); var_dump (intval ($ )) let's take a look at the following online test code:

The code is as follows:


$ A = 9.45*100;
Var_dump ($ );
Var_dump (intval ($ ));
$ A = 945*1.00;
Var_dump ($ );
Var_dump (intval ($ ));
?>


Running result: float (945) int (944) float (945) int (945)
This code tells the results, but many people still don't understand it. this doesn't explain why unexpected transformations happen.
All the online statements about this situation are ambiguous. Here is a simple explanation:
The number 9.45 is shown here, but it is not in the machine, but 44999999999999999 .... Therefore:
9.449999*100 = 944.9999. So you can understand it? Intval removes the ending number directly. this is the name of Shenma. in this case, intval and floor () functions are similar. Haha. This is something I didn't notice before. I didn't notice that intval will be rounded down.
And 1.00 is nothing like 1.0099999, so 945*1.00 will have a float 945. the intval transformation will naturally not happen 944.
There are also some typical questions, such as intval (0.1 + 0.7) * 10) equals 7 rather than 8. This is all true. Okay. This is a simple example. At least it is much better than a lot of Alibaba Cloud on the Internet.

The pipeline code is as follows :? Php $ a = 9.45*100; var_dump ($ a); var_dump (intval ($ a); $ a = 945*1.00; var_dump ($ ); var_dump (intval ($ ))...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.