Due to the precision of the PHP floating point calculation, the order amount is often less than 1 point, and the order is less than 1

Source: Internet
Author: User

Due to the precision of the PHP floating point calculation, the order amount is often less than 1 point, and the order is less than 1

Recently, I encountered a strange problem: the mall often pays less than a penny for the Order, which was caused by the precision of the PHP floating point operation.

It is caused by the precision of the PHP floating point operation. Bolg of laruence has a detailed description. Http://www.laruence.com/2013/03/26/2884.html,
When decimal places are expressed in binary, 0.58 is an infinitely long value for binary.

The binary representation of 0.58 is basically (52 bits): the binary representation of 0010100011110101110000101000111101011100001010001111 is basically (52 bits): 0.57

Convert to a floating point number (64-bit dual-precision)

0.58-> 0.57999999999999996 0.57-> 0.56999999999999995
0.58*100 = 57.999999999 (int) (0.58*100) = 57

Solution:

(Int) (0.58*1000)/10) = 58

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.