Why is the result 0 displayed as 3.5527136788005E-15 in php?

Source: Internet
Author: User
Why is the result 0 displayed as 3.5527136788005E-15 $ yuejie = 0 in php;
$ Yuejie + = $ v ['yuejie '];
$ V ['yuejie ']


Add result



The data in the third row should be 0, but 3.5527136788005E-15 is displayed. why?


Reply to discussion (solution)

Are you all asleep?

No data code, conjecture ......
Look at echo PHP_INT_MAX; what is output?

3.5527136788005E-15 is a very small number (I don't even know the scientific notation). we can think that it is 0.
As for how it is not true 0, please review the representation of numbers in basic computer knowledge.
In addition, please also review the knowledge of valid numbers.

3.5527136788005E-15 is a very small number (I don't even know the scientific notation). we can think that it is 0.
As for how it is not true 0, please review the representation of numbers in basic computer knowledge.
In addition, please also review the knowledge of valid numbers.


I just don't understand. the cumulative result should be 0. Why does it look like this? In addition, my data is composed of two decimal places. how does it look like this?

3.5527136788005E-15 is a very small number (I don't even know the scientific notation). we can think that it is 0.
As for how it is not true 0, please review the representation of numbers in basic computer knowledge.
In addition, please also review the knowledge of valid numbers.


Dear user, let me explain how to solve this problem. I have encountered this problem during my project. if I go to the book to study, it will be too late.

The toFixed method returns a string representing a number in fixed-point notation. NumObj. toFixed ([fractionDigits]): Required for numObj. A Number object. FractionDigits option. The number of digits after the decimal point. The value must be 0? 20, including 0 and 20. The toFixed method returns a string of numbers in fixed-point notation. This string contains a valid number before the decimal point, and must contain the fractionDigits number. If the fractionDigits parameter is not available or the parameter is undefined, the toFixed method assumes that the value is 0.

Retain 2 decimal places for the data you display.
a = -80.27;b = 53.51;c = 26.76;document.write((a+b+c).toFixed(2));
0.00

Yo, wrong! I thought it was a js issue.
The calculation accuracy issues do not need to be excessively considered during calculation; otherwise, the error accumulation will be faster.
You only need to handle the appropriate precision during display.

$yuejie = 0;$a = array(  array('yuejie' => -80.27),  array('yuejie' => 53.51),  array('yuejie' => 26.76),  array('yuejie' => -38.29),  array('yuejie' => 38.29),);foreach($a as $v) {  $yuejie += $v['yuejie'];  printf("%8.2f %8.2f\n", $v['yuejie'], $yuejie);}

  -80.27   -80.27   53.51   -26.76   26.76     0.00  -38.29   -38.29   38.29     0.00

After the problem is solved, the result is rounded off and the decimal point is 0.

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.