This article mainly introduces the human headache in PHP in the floating-point arithmetic, combined with the example of the use of PHP for floating-point operation round function, the need for friends can refer to the following
In this paper, we analyze the floating-point arithmetic in PHP which makes people headache. Share to everyone for your reference, as follows:
When doing e-commerce, calculate the price is unavoidable, and then found a hole in PHP, mental arithmetic 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
The code is modified to:
$price =69.1; $count =100; $total =round ($price * $count) -6910;echo $total;
Problem solved, the output is 0, correct
I hope this article is helpful to you in PHP programming.