# Javascript of js decimal multiplication and division problem

• 2020-03-30 02:19:36
• OfStack

One, with js calculation
12.32 * 7   So what do we get? Answer: 86.24000000000001

Why this problem? How?
Js in dealing with decimal multiplication and division method has a bug, the solution can be: decimal into integer to deal with.
The above calculation can be changed to:
12.32 times 100 times 7/100
The result is 86.24, correct.

Another calculation:
8.80 times 100 times 12/100
Results: 105.60000000000002
38.80 a similar problem will arise.

Accuracy increased 10 times:
8.80 times 1000 times 12/1000
Results: 105.6
To normal.

16.40 * 1000000 * 6/1000000
The results are problematic

In order to make js execution more accurate, in the later js decimal calculation directly to the value of 10000 times, and then divided by 10000, you can solve the problem.
Var num = 38.80;
Var num2 = 13;
Alert (num * 10000 * 12/10000);

The number multiplied and divided by 10,000 is tested to be the most suitable. Some Numbers are wrong when they are small, and some Numbers are wrong when they are large (1000000).

Second,

``````
<script defer>
Number.prototype.rate=function(){
varoStr=this.toString();
if(oStr.indexOf(".")==-1)
return1;
else
returnMath.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
}
</script>
``````

The solution is cumbersome, but it gives you an idea of the actual process of solving the problem.
``````
//Division function, used to get accurate division results
//Note: javascript division results will have errors, in two floating point division will be more obvious. This function returns a more precise 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 to make it easier to call.
Number.prototype.div = function (arg){
return accDiv(this, arg);
}
//The multiplication function, which is used to get exact multiplication results
//Note: javascript multiplication results will have errors, in the two floating point number multiplication will be more obvious. This function returns a more precise multiplication result.
//Call: accMul (arg1, arg2)
//Return value: the exact result of arg1 times 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 to make it easier to call.
Number.prototype.mul = function (arg){
return accMul(arg, this);
}
//Note: javascript addition results will have errors, in the two floating point Numbers will be more obvious when adding. This function returns a more accurate addition result.
//Return value: the exact result of arg1 plus 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 to make it easier to call.
}
``````

Related articles: