Solution of incorrect value of Number after numerical operation in js

  • 2021-07-24 09:48:45
  • OfStack

Question:

37.5*5.5=206.08 (JS is calculated as a result, and I take two decimal places by 4 rounding 5)

I first suspected that it was a problem of 4 rounds and 5 entries, so I calculated a result directly with JS: 206.084999999999998

How can this happen? When two numbers with only one decimal place multiply, how can there be so many decimal points?

I did 1 Google and found that this is an bug of JavaScript floating-point operation.

For example: 7*0. 8 JavaScript is calculated as: 5.600000000000000005

Solution: I found a solution on the Internet, that is, I rewrote a function of floating-point operation.

These methods are excerpted below for reference by friends who encounter the same problems:

Program code


// 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 more convenient to call. 
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);
}

Subtraction function


function accSub(arg1, arg2) {
var r1, r2, m, n;
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));
//last modify by deeka
// Dynamic control precision length 
n = (r1 >= r2) ? r1 : r2;
return ((arg1 * m - arg2 * m) / m).toFixed(n);
}

Include these functions where you want to use them, and then call them to evaluate them.

For example, if you want to calculate: 7*0. 8, change it to (7). mul (8)

Other operations are similar, and more accurate results can be obtained.


Related articles: