How to solve multiple decimal places in Javascript decimal operations? Multiple decimal places in js decimal operations

Source: Internet
Author: User

How to solve multiple decimal places in Javascript decimal operations? Multiple decimal places in js decimal operations

Share with you an interesting test:

0.1 + 0.2 = 0.3 // false
Suddenly depressed, okay! 0.1 + 0.2 changed to: 0.30000000000000004
Another 2.4/0.8 => 2.9999999999999996 cannot be converted into an integer (2.4*100)/(0.8*100)
10.22 now I want to subtract the result value of 0.11 and then there will be a lot of decimal places 10.110000000000001. Then I used the toFixed method to filter decimal places, but I don't know which efficiency is high to convert it to an integer! Let's try again!

Var date1 = new Date (); for (var I = 0; I <10000; I ++) {var result1 = (10.22-0.11 ). toFixed (2);} alert (new Date ()-date1); // low efficiency var date2 = new Date (); for (var j = 0; j <10000; j ++) {var result2 = (10.22*1000-0.11*1000)/1000;} alert (new Date ()-date2 ); // High Efficiency alert (0.1 + 0.2 = 0.3); // Since falsealert (0.1 + 0.2) is returned; // Since 0.30000000000000004 alert (parseFloat (0.1) is returned) + parseFloat (0.2); // returns 0.30000000000000004

I checked some information. One is the Bug of JavaScript floating-point number calculation, and the other is related to the computer's final conversion to binary calculation. But why not all decimals will have this problem? I am not sure yet, I have time to study it in depth.

Solution:
To solve this problem, the first method is to use the toFixed (n) method of JavaScript to directly obtain N decimal places. However, I think this method has some problems in data precision. If the data accuracy requirement is not high, you can use it.

alert((0.1 + 0.2).toFixed(1));

The second method is to write the calculation method by yourself. The following is a user-defined addition function. Using this method for addition will avoid the above problem.

// UDF addNum (num1, num2) {var sq1, sq2, m; try {sq1 = num1.toString (). split (". ") [1]. length;} catch (e) {sq1 = 0;} try {sq2 = num2.toString (). split (". ") [1]. length;} catch (e) {sq2 = 0;} m = Math. pow (10, Math. max (sq1, sq2); return (num1 * m + num2 * m)/m;} alert (addNum (0.1, 0.2 ));

Of course, you can also write it as follows: alert (num * 3 + 10*3)/3). N decimal places are not displayed.
Alert (num * 3 + 10*3)/3); and alert (num + 10); There is a difference between the two programming languages: Converting computers to binary operations at the underlying layer, perhaps this is the reason for the above problem. We will discuss it further.

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.