A summary of JavaScript digital accuracy loss

Source: Internet
Author: User

This article is divided into three parts:

1, JS digital precision loss of some typical problems

2, the reason for the loss of JS digital precision

3. Solutions (one object + one function)

First, JS Digital precision loss of some typical problems

1. Two simple floating-point numbers added

0.1 0.2 0.3 // true

Look at the results of Java operations

2. Large integer operation

9999999999999999 10000000000000001 // true var 9007199254740992  1//  true

The 16-and 17-digit numbers are equal, plus 1 are equal.

3. ToFixed will not be rounded (Chrome)

1.335. toFixed (2//  1.33

There has been an inconsistency between the price in Chrome and other browsers on the line because of the toFixed compatibility issue.

second, JS digital loss of precision reasons

The binary implementation of the computer and the number of bits limit cannot be expressed in finite numbers. Just like some irrational numbers can not be limited to express, such as Pi 3.1415926...,1.3333 ... such as JS follows the IEEE 754 specification, using double-precision storage (double precision), occupying a single bit.

A summary of JavaScript digital accuracy loss

Related Article

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.