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

Source: Internet
Author: User
In php, the calculation result is 0. why is the post 3.5527136788005E-15 finally edited by vbker at 2014-07-2023:30:47 $ yuejie & nbsp; 0; $ yuejie & nbsp; + & nbsp; $ v [yuejie]; $ v [yuejie] & nbsp; the result of data input to php is 0. why is the result displayed as 3.5527136788005E-15?

This post was last edited by vbker at 23:30:47

$ Yuejie = 0;
$ Yuejie + = $ v ['yuejie '];
$ V ['yuejie ']


Add result



The data in the third row should be 0, but 3.5527136788005E-15 is displayed. why?
------ Solution --------------------
No data code, conjecture ......
Look at echo PHP_INT_MAX; what is output?
------ Solution --------------------
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.
------ Solution --------------------
ToFixed method
Returns a string representing a number in fixed-point notation.

NumObj. toFixed ([fractionDigits])

Parameters
NumObj

Required. A Number object.

FractionDigits

Optional. The number of digits after the decimal point. The value must be between 0 and 20, including 0 and 20.

Description
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

------ Solution --------------------
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

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.