This article describes how to calculate and compare floating-point numbers in PHP and how to obtain an inaccurate integer. The code is very simple. For more information, see
This article describes how to calculate and compare floating-point numbers in PHP and how to obtain an inaccurate integer. The code is very simple. For more information, see
The interesting phenomenon of php should be that many programming languages have such a phenomenon. 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.