Home >Web Front-end >JS Tutorial >How to solve the problem of multiple decimal places in js decimal operation
Regarding the problem of multiple decimal places when adding, subtracting, multiplying, and dividing decimals in JavaScript, I studied it myself and conducted relevant tests. I gained a lot of knowledge points. Friends in need can refer to it.
Let me share with you an interesting test:
0.1 0.2 == 0.3 //false
I was suddenly depressed, okay! It turns out that 0.1 0.2 becomes: 0.30000000000000004
Another 2.4/0.8 => 2.9999999999999996 There is no way to change it, and convert it into an integer (2.4 * 100)/(0.8 * 100)
10.22 Now we need to subtract 0.11 to get the result There were a lot of decimals in the value, 10.110000000000001, and then I used the toFixed method to filter the decimals, but I don’t know which one is more efficient than the previous method of converting it into an integer and then executing it. 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);//效率低 var date2 = new Date(); for(var j = 0; j < 10000; j++){ var result2 = (10.22 * 1000 - 0.11 * 1000) / 1000; } alert(new Date() - date2);//效率高 alert(0.1 + 0.2 == 0.3); //既然返回false alert(0.1 + 0.2); //既然返回0.30000000000000004 alert(parseFloat(0.1) + parseFloat(0.2)); //还是返回0.30000000000000004
I checked some information, one is a bug in JavaScript floating point calculation, and the other is related to the final conversion of the computer into binary calculation, but why not all decimals This phenomenon will always occur. I don’t know it yet. I will study it in depth when I have time.
Solution:
There are two ways to solve this problem. The first is to use JavaScript's toFixed(n) method to directly obtain N decimal places. , however, I personally feel that this method will have some problems in data accuracy. 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 the calculation method yourself. The following is a custom addition function. Using this method for addition will avoid the above problems.
//自定义加法运算 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, it can also be written simply as: alert((num * 3 10 * 3) /3); In this way, N multiple decimal places will not appear.
alert((num * 3 10 * 3) /3); and alert(num 10); There is a difference between these two writing methods when the computer converts them into binary operations at the bottom level. Perhaps this is the reason for the above problem, or We need to study it in depth and you can discuss it more.
The above is the entire content of this chapter. For more related tutorials, please visit JavaScript Video Tutorial!