js-Decimal Calculation problem

Source: Internet
Author: User

First:

What's the situation?

Reason: JS uses binary for fractional calculation

First look at the decimal conversion to binary binary method:

The integer digits of the decimal number are the integer digits of the binary number, and the decimal digit is the scale of the binary number.

If we have a decimal 111.4 (10), we'll calculate it for him:

First take the decimal digit 0.4, the "multiply two take the whole method"

0.4*2=0.8 the number of the integer position of the result is 0 | By

0.8*2=1.6 Take 1 | Shun

0.6*2=1.2 Take 1 | Order

0.2*2=0.4 Take 0 | Write

0.4*2=0.8 Take 0 | Out

0.8*2=1.6 Take 1 |

0.6*2=1.2 Take 1 |

0.2*2=0.4 Take 0 |

Come here, we find that there is a loop, so we can infer that the binary after the decimal point is

0.01100110 ... (Cycle 0110)

The whole number is not written, except for the 2 method of taking the remainder

The problem of 2.6+2.6+2.6=7.800000000001,javascript in the calculation of decimal operations, the decimal number will be converted to the corresponding binary, part of the decimal can not be fully converted to binary, where the first error occurs. After the decimal is converted to binary, the binary operation is performed, and the binary result is obtained. The binary results are then converted to decimal, where the second error is usually present.

Workaround: Specify the number of decimals (7.8) to keep. ToFixed (2) = 7.80; This method has the potential to lose precision, and there is a way to customize this addition on the Web, plus a link

A solution to the problem of multiple decimal places in JS decimal operation

js-Decimal Calculation problem

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.