1. Use js to calculate
12.32 * 7 What is the result?
Answer: 86.240000000000001
Why does this problem occur? How to solve it?
There is a bug when js deals with multiplication and division of decimals. The solution can be: turn the decimal into an integer to handle it.
The above calculation can be changed to:
12.32 * 100 * 7 /100
The result is: 86.24, correct.
Let's calculate it again:
8.80 * 100 * 12 / 100
Result: 105.600000000000002
Similar problems will occur at 38.80.
Increase accuracy by 10 times:
8.80 * 1000 * 12 / 1000
Results: 105.6
It's normal.
16.40 * 1000000 * 6 / 1000000
There are problems with the result
In order to make js execution more accurate, the value can be directly expanded by 10,000 times in the subsequent js decimal calculation, and then divided by 10,000 to solve the problem.
var num = 38.80;
var num2 = 13;
alert(num * 10000 * 12 / 10000);
The number multiplied and divided by 10,000 is the most suitable for testing. Some numbers have problems when they are smaller, and some numbers have problems when they are larger (1000,000).
two,
<script>Number.prototype.rate=function(){varoStr=this.toString();if(oStr.indexOf(".")==-1)return1;elsereturnMath.pow(10,parseInt(oStr.length-oStr.indexOf(".")-1));} functiontran(){args=tran.arguments;vartemp=1;for(i=0;i<args.length;i++)temp*=args[i]*args[i].rate();for(i=0;i<args.length;i++)temp/=args[i].rate();returntemp} alert(tran(11,22.9)); </script>This solution is a more cumbersome solution, but it can give you a rough understanding of the actual process of solving this problem.
//Divide function is used to obtain accurate division results //Note: There will be errors in the division result of javascript, which will be more obvious when dividing two floating point numbers. This function returns a more accurate division result. //Call: accDiv(arg1,arg2)//Return value: the exact result of arg1 divided by arg2 function accDiv(arg1,arg2){var t1=0,t2=0,r1,r2;try{t1=arg1.toString().split(".")[1].length}catch(e){}try{t2=arg2.toString().split(".")[1].length}catch(e){}with(Math){r1=Number(arg1.toString().replace(".","))r2=Number(arg2.toString().replace(".","))return (r1/r2)*pow(10,t2-t1);}} //Add a div method to the Number type, making it more convenient to call. Number.prototype.div = function (arg){return accDiv(this, arg);} //Multiple function is used to obtain accurate multiplication results//Note: There will be errors in the multiplication results of javascript, which will be more obvious when multiplying two floating-point numbers. This function returns a more accurate multiplication result. //Call: accMul(arg1,arg2)//Return value: the exact result of arg1 multiplied by arg2 function accMul(arg1,arg2){var m=0,s1=arg1.toString(),s2=arg2.toString();try{m+=s1.split(".")[1].length}catch(e){}try{m+=s2.split(".")[1].length}catch(e){}return Number(s1.replace(".","))*Number(s2.replace(".","))/Math.pow(10,m)} // Add a mul method to the Number type, which is more convenient to call. Number.prototype.mul = function (arg){return accMul(arg, this);} //Addition function is used to obtain accurate addition results//Note: There will be errors in the addition result of javascript, which will be more obvious when adding two floating-point numbers. This function returns a more accurate addition result. //Call: accAdd(arg1,arg2)// Return value: the exact result of arg1 plus arg2 function accAdd(arg1,arg2){var r1,r2,m;try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}m=Math.pow(10,Math.max(r1,r2))return (arg1*m+arg2*m)/m} //Add an add method to the Number type, making it more convenient to call. Number.prototype.add = function (arg){return accAdd(arg,this);}The above javascript decimal multiplication result error handling is all the content I have shared with you. I hope you can give you a reference and I hope you can support Wulin.com more.