How to solve the _javascript technique for the number of decimal places in JS decimal operation

Source: Internet
Author: User
Tags date1

Share an interesting test with everyone:

0.1+0.2 = = 0.3//false
Suddenly depressed, OK! The original 0.1+0.2 into: 0.30000000000000004
One more 2.4/0.8 =>2.9999999999999996 no way to change it, all of them are converted to integers (2.4 * 100)/(0.8 * 100)
10.22 now to subtract 0.11 results and a lot of decimal 10.110000000000001, and then I used the ToFixed method to filter decimals, but I do not know the same as before the conversion to an integer after the implementation of the high efficiency, good! Let's try it again!

var date1 = new Date ();
for (var i = 0; I < 10000 i++) {
 var result1 = (10.22-0.11). toFixed (2);    
}
Alert (new Date ()-date1);//Inefficient
  
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 return False
alert (0.1 + 0.2); 4
Alert (parsefloat (0.1) + parsefloat (0.2));//Return 0.30000000000000004

Some data, one is a JavaScript floating-point calculation of the Bug, and the computer eventually converted into binary calculation, but why not all decimal will have this phenomenon, at present I do not know, have time to further study.

Workaround:
two ways to solve this problem, the first is to use the JavaScript tofixed (n) method, directly to get n decimal places, but the individual felt that this method in the data accuracy of some problems. It can be used if the data accuracy requirements are not high.

Alert ((0.1 + 0.2). ToFixed (1));

The second method is to write your own method of operation. The following is a custom addition function, which is added by using this method to avoid the above problem.

Custom addition operation
function 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, simplicity can also be written as: alert (num * 3 + 10 * 3)/3); This also does not appear N multiple decimal places.
Alert ((NUM * 3 + 10 * 3)/3); With alert (num + 10); These two kinds of writing computer at the bottom of the conversion into binary operation is different, perhaps this is the cause of the above problems, we still have to go into the in-depth study, we can discuss a lot.

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.