Js for decimal addition precision processing example

  • 2020-03-30 01:05:51
  • OfStack

 
zf = accAdd(zf, parseFloat("12.11")); 
//The addition function, used to get accurate addition results
//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.
//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; 
} 

Related articles: