In this paper, we analyze some of the frustrating floating-point arithmetic in PHP. Share to everyone for your reference, specific as follows:
In the electrical business, the calculation price is unavoidable, and then found a hole in PHP, multi-digit should be the correct value, PHP operation will not be the same as you
Take a look at the following code:
 
$price = 69.1;
$count = 100;
$total = $price * $count-6910;
Echo $total;
 
Guess what the value of the variable $total is, run this code output: -9.09494701773E-13
How to solve this problem?
Using the ROUND function
 
Code modified to:
 
$price = 69.1;
$count = 100;
$total =round ($price * $count)-6910;
Echo $total;
 
Comparison of calculation results of floating point numbers
A floating-point count example is as follows:
$a = 0.2+0.7;
$b = 0.9;
Var_dump ($a = = $b);
 
The result is: BOOL (false). In other words, 0.2+0.7 's calculations here are not equal to 0.9, which is clearly contrary to our common sense.
In this case, the official PHP manual has also stated that it is clear that simple decimal scores such as 0.2 cannot be converted to an internal binary format without losing a little bit of precision. This is related to the fact that it is impossible to accurately express certain decimal points with a finite number of digits. For example, the decimal 1/3 becomes 0.3333333 ....
We print the above variables in a double format:
 
$a = 0.2+0.7;
$b = 0.9;
printf ("%0.20f", $a);
echo ' <br/> ';
printf ("%0.20f", $b);
 
The output results are as follows:
0.89999999999999991118
0.90000000000000002220
 
Obviously here, in fact, as floating-point data, its precision has been lost a part, not fully accurate. So never believe that floating-point numbers are accurate to the last one, and never compare two floating-point numbers for equality. What needs to be explained is that this is not a PHP problem, but the problem of dealing with floating-point numbers inside the computer! The same problem can be found in languages such as C, JAVA, and so on.
So to compare two floating-point numbers, you need to control them in the range of precision we need to compare them, so use the Bcadd () function to add and make the precision conversion (for string) of floating-point numbers:
 
Var_dump (Bcadd (0.2,0.7,1) = = 0.9); Output: bool (TRUE)
 
Floating-point number rounding
In the article "PHP takes whole function ceil and floor", there are examples:
 
<?php
echo ceil (2.1/0.7); Output: 4
?>
 
After the calculation of floating point numbers, we know that this is caused by the incomplete accuracy of the floating-point calculation results:
 
<?php
printf ("%0.20f", (2.1/0.7)); Output: 3.00000000000000044409
?>
After the discussion of the calculation of floating point number, we know that this is caused by the incomplete precision of the floating-point calculation result, so we can use the round () function to deal with it:
<?php
Echo ceil (Round (2.1/0.7), 1);
?>
 
Although the round () function is rounded in accordance with the specified precision, reserving one digit after the decimal point does not affect our rounding result.