Treatment of Errors in javascript Decimal Multiplication Results

  • 2021-07-06 09:50:00
  • OfStack

1. Calculate with js

12.32 * 7 What is the result?
Answer: 86.24000000000001

Why is there such a problem? How to solve it?
js has one bug when dealing with the multiplication and division of decimals. The solution can be to change decimals into integers.
The above calculation can be changed to:
12.32 * 100 * 7 /100
The result is 86.24, correct.

In addition, calculate another 1:
8.80 * 100 * 12 / 100
Results: 105.60000000000002
38.80 A similar problem arises.

Accuracy increased by 10 times:
8.80 * 1000 * 12 / 1000
Results: 105.6
It's normal.

16.40 * 1000000 * 6 / 1000000
The results are also problematic

In order to make js more accurate, the problem can be solved by directly expanding the value by 10000 times and dividing it by 10000 in the future js decimal calculation.
var num = 38.80;
var num2 = 13;
alert(num * 10000 * 12 / 10000);

The number that is multiplied and divided has been tested to be 10000. If it is small, some numbers will go wrong, and if it is large (1000000), some numbers will go wrong.

2.


<script>
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
}
 
alert(tran(11,22.9));
 
</script>

This solution is rather troublesome, but it gives you a general idea of the actual process of solving this problem.


// Division function, which is used to get accurate division results 
// Description: javascript There will be errors in the division results of two floating-point numbers, which will be obvious when dividing two floating-point numbers. This function returns a more accurate division result. 
// Call: accDiv(arg1,arg2)
// Return value: arg1 Divide by arg2 Accurate results of 
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);
}
}
 
// To Number Type increase 1 A div Method, which is called more   Convenient. 
Number.prototype.div = function (arg){
return accDiv(this, arg);
}
 
// Multiplication function, which is used to get accurate multiplication results 
// Description: javascript There will be errors in the multiplication results of two floating-point numbers, which will be obvious when two floating-point numbers are multiplied. This function returns a more accurate multiplication result. 
// Call: accMul(arg1,arg2)
// Return value: arg1 Multiply by  arg2 Accurate results of 
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)
}
 
//  To Number Type increase 1 A mul Method, which is more convenient to call. 
Number.prototype.mul = function (arg){
return accMul(arg, this);
}
 
// Addition function, which is used to get accurate addition results 
// Description: javascript There will be errors in the addition results of two floating-point numbers, which will be obvious when two floating-point numbers are added. This function returns a more accurate addition result. 
// Call: accAdd(arg1,arg2)
//  Return value: arg1 Plus arg2 Accurate results of 
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
}
 
// To Number Type increase 1 A add Method, which is more convenient to call. 
Number.prototype.add = function (arg){
return accAdd(arg,this);
}

Related articles: