This article mainly introduces the troublesome floating-point calculation in php, and analyzes the usage of php's round function for floating-point calculation in the form of examples, for more information about how to use the round-robin function in php, see this article.
This article analyzes the headaches of floating point calculation in php. We will share this with you for your reference. The details are as follows:
When doing e-commerce, it is inevitable to calculate the price, and then find a pitfall in php, the correct value should be calculated, the php operation will be different from you
See the following code:
$ Price = 69.1; $ count = 100; $ total = $ price * $ count-6910; echo $ total;
You can guess the value of the variable $ total and run the code to output:-9.09494701773E-13.
How can this problem be solved?
Use the round function
Modify the code:
$ Price = 69.1; $ count = 100; $ total = round ($ price * $ count)-6910; echo $ total;
The problem is solved, and the output result is 0, correct
I hope this article will help you with PHP programming.
For more articles about floating-point calculation and analysis in php, please refer to the Chinese PHP website!