Analysis on the headache of floating-point calculation in php

Source: Internet
Author: User
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!

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.