To sum up the considerations for numerical calculation in PHP, the php Value
1. round-round the floating point number
float round ( float $val [, int $precision ] )
2: floor-rounding (down)
float floor ( float $value )
3. ceil-one-to-one method to get an integer (rounded up)
float ceil ( float $value )
Pitfalls:When the value is an integer, for example, 11, floor (11) = 10, ceil (11) = 12; the problem is obvious, so pay special attention to this during calculation.
Solution:Floor (11 + 0.01 );
2. multiplication and division of integers and Decimals
Pitfalls:3.5*100 = 300, even if you have good mathematics, it is useless. In the php world, this is the truth. If you use php's +-*/to calculate floating point numbers, some problems may occur due to incorrect computing results, such as echo intval (0.58*100), printing 57 instead of 58. This is actually a bug where the underlying binary of the computer cannot accurately represent floating point numbers, it is cross-language. I also encountered this problem using python. Therefore, most languages provide precision computing class libraries or function libraries. For example, php has a high-precision BC function library.
Solution:Is to change the integer to floating point 3.5 * (float) 100 = 350
Third, the floating point is weird.
Pitfalls:8.50-8 = 0.500000001 in fact, the reason is similar to 2
Solution:Round () the result to improve accuracy.
The above is all the considerations for PHP Data computing. I hope this article will help you with data computing in PHP.