The _php technique of floating point arithmetic analysis with headache in PHP

Source: Internet
Author: User
Tags arithmetic php programming php regular expression

In this paper, we analyze some of the frustrating floating-point arithmetic in PHP. Share to everyone for your reference, specific as follows:

In the electrical business, the calculation price is unavoidable, and then found a hole in PHP, multi-digit should be the correct value, PHP operation will not be the same as you

Take a look at the following code:

$price =69.1;
$count =100;
$total = $price * $count -6910;
Echo $total;

Guess what the value of the variable $total is, run this code output: -9.09494701773E-13
How to solve this problem?

Using the ROUND function

Code modified to:

$price =69.1;
$count =100;
$total =round ($price * $count) -6910;
Echo $total;

The problem is solved, the output is 0, correct.

More about PHP Interested readers can view the site topics: "PHP Mathematical Arithmetic Skills summary", "PHP array" operation Skills Daquan, "PHP Sorting algorithm Summary", "PHP commonly used traversal algorithm and skills summary", "PHP Data structure and algorithm tutorial", " PHP Programming algorithm Summary, "PHP Regular Expression Usage summary", "PHP operation and operator Usage Summary", "PHP string (String) Usage summary" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

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.