Home >Web Front-end >JS Tutorial >js example of precision processing of decimal addition_javascript skills

js example of precision processing of decimal addition_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:06:401318browse
Copy code The code is as follows:

zf = accAdd(zf, parseFloat("12.11"));
//Addition function, used to obtain accurate addition results
//Explanation: The addition result of JavaScript will have errors, 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;
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn