This article describes how to calculate and compare floating-point numbers in PHP and how to obtain inaccurate integers. The code is very simple. If you need it, you can refer to the interesting phenomenon in php, this should happen in many programming languages. This is because of the floating point number recognition problem of the computer itself... the following code will show you:
$ F = 0.58; var_dump (intval ($ f * 100*100); // result 5799 var_dump (float) ($ f * 100*100 )); // result 5800 echo (int) (0.1 + 0.7) * 10); // result 7 echo (float) (0.1 + 0.7) * 10 ); // result 8 <? Php $ a = 0.1; $ B = 0.7; var_dump ($ a + $ B) = 0.8); // The printed value is boolean false <? Php $ a = 0.1; $ B = 0.7; var_dump (bcadd ($ a, $ B, 2) = 0.8); // bool true
Note that during Floating Point calculation, do not convert a floating point to an integer. Otherwise, an unanticipated error may occur.
The above code is a solution to the calculation and comparison of floating point numbers in PHP and the inaccuracy of integer obtaining.