Computational analysis of floating-point numbers in PHP that make people headache

Source: Internet
Author: User
Tags php programming
This article mainly introduces the human headache in PHP in the floating-point arithmetic, combined with the example of the use of PHP for floating-point operation round function, the need for friends can refer to the following

In this paper, we analyze the floating-point arithmetic in PHP which makes people headache. Share to everyone for your reference, as follows:

When doing e-commerce, calculate the price is unavoidable, and then found a hole in PHP, mental arithmetic 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

The code is modified to:

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

Problem solved, the output is 0, correct

I hope this article is helpful to you in PHP programming.

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.