PHP evaluates to zero, why it is displayed as 3.5527136788005E-15

Source: Internet
Author: User
PHP evaluates to 0, why it is displayed as 3.5527136788005E-15
This post was last edited by Vbker on 2014-07-20 23:30:47

$yuejie = 0;
$yuejie + = $v [' Yuejie '];
$v [' Yuejie '] data into


Accumulate results into



The third row of data should be 0, but it shows 3.5527136788005E-15, which is why?
------Solution--------------------
No data code, guess ...
Look at Echo Php_int_max; Output what?
------Solution--------------------
3.5527136788005E-15 is a very small number (not even the scientific notation), and we can think of him as 0.
As for how is not the real 0, then ask you to review the computer in the basic knowledge of the representation of the number
Also please review the knowledge of the effective number
------Solution--------------------
ToFixed method
Returns a String that represents a number represented by a fixed-point notation.

Numobj.tofixed ([fractiondigits])

Parameters
Numobj

Required option. A number object.

FractionDigits

Options are available. Number of digits after the decimal point. Its value must be between 0–20, including 0 and 20.

Description
The ToFixed method returns a string representation of a number in fixed-point notation. There is a valid number before the decimal points of the string, and must then contain fractiondigits digits.

If there is no fractiondigits parameter, or the parameter is the Undefined,tofixed method, the value is assumed to be 0.


Keep 2 decimal places for the data you are displaying
A =-80.27;
b = 53.51;
c = 26.76;

document.write ((a+b+c). toFixed (2));
0.00

------Solution--------------------
Yo, it's wrong! I thought it was a JS problem.
The calculation accuracy problem is calculated without undue consideration, otherwise the error accumulates faster
Just handle the right precision when you display it.
$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.