Home > 类库下载 > PHP类库 > body text

Due to the accuracy of PHP floating point calculations, the order amount is often less than 1 cent.

高洛峰
Release: 2016-10-10 11:24:15
Original
1375 people have browsed it

Due to the accuracy of PHP floating point operations, the order amount is often less than 1 cent.

I recently encountered a strange problem. Orders paid through WeChat in the mall are often less than 1 cent. After troubleshooting, it is caused by the accuracy of PHP floating point operations.

It is caused by the precision of PHP floating point number operations. Brother Niao’s Bolg has detailed instructions. http://www.laruence.com/2013/03/26/2884.html,
When decimals are expressed in binary, 0.58 is an infinite value for binary

0.58的二进制表示基本上(52位)是: 0010100011110101110000101000111101011100001010001111
0.57的二进制表示基本上(52位)是: 0010001111010111000010100011110101110000101000111101
Copy after login

Convert to floating point number (64-bit double precision)

0.58 -> 0.57999999999999996
0.57 -> 0.56999999999999995
Copy after login
rrree

Solution:

0.58*100 = 57.999999999 
(int)(0.58*100) = 57
Copy after login

Due to the accuracy of PHP floating point calculations, the order amount is often less than 1 cent.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template