简介js数字计算丢失精度问题解决方案计算机世界里,数字的计算,所有语言都会丢失精度,所以没有万全之策,但在人力范围内,尽量解决。网上找了一部分代码,发现是有问题的,比如://加法Number.prototype.myAdd=function(arg2){vararg1=this;if(isNaN(arg2)){returnarg2;}varr1,r2,m;try{r1=arg1.toString().
js数字计算丢失精度问题解决方案
计算机世界里,数字的计算,所有语言都会丢失精度,所以没有万全之策,但在人力范围内,尽量解决。
网上找了一部分代码,发现是有问题的,比如:
//加法Number.prototype.myAdd=function(arg2){vararg1=this;if(isNaN(arg2)){returnarg2;}varr1,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}//减法Number.prototype.mySub=function(arg2){vararg1=this;if(isNaN(arg2)){returnarg2;}varr1,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));n=(r1>=r2)?r1:r2;return((arg1*m-arg2*m)/m).toFixed(n);}//乘法Number.prototype.myMul=function(arg2){vararg1=this;if(isNaN(arg2)){returnarg2;}varm=0,s1=arg1.toString(),s2=arg2.toString();try{m+=s1.split(".")[1].length}catch(e){}try{m+=s2.split(".")[1].length}catch(e){}returnNumber(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)}//除法Number.prototype.myDiv=function(arg2){vararg1=this;if(isNaN(arg2)){returnarg2;}vart1=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).myMul(pow(10,t2-t1))}}
在计算一些特殊的数字时,仍然有问题:
比如加法:
268.34.myDiv(0.83);//321.7505995203837
所以还要优化
我重新做了一版:
varoperationNumber=function(arg1,arg2,operator){varoper=['+','-','*','/'];//不合法的运算if(isNaN(arg1)||isNaN(arg2)||oper.indexOf(operator)<0){returnNaN;}//除以0if(operator==='/'&&Number(arg2)===0){returnInfinity;}//和0相乘if(operator==='*'&&Number(arg2)===0){return0;}//相等两个数字相减if((arg1===arg2||Number(arg1)===Number(arg2))&&operator==='-'){return0;}varr1,r2,max,_r1,_r2;try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}max=Math.max(r1,r2)_r1=max-r1;_r2=max-r2;if(_r1!==0){arg1=arg1+'0'.repeat(_r1)}if(_r2!==0){arg2=arg2+'0'.repeat(_r2)}arg1=Number(arg1.toString().replace('.',''))arg2=Number(arg2.toString().replace('.',''))varr3=operator==='*'?(max*2):(operator==='/'?0:max);varnewNum=eval(arg1+operator+arg2);if(r3!==0){varnStr=newNum.toString();nStr=nStr.replace(/^-/,'');if(nStr.length<r3+1){nStr='0'.repeat(r3+1-nStr.length)+nStr;}nStr=nStr.replace(newRegExp('(\d{'+r3+'})$'),'.$1');if(newNum<0){nStr='-'+nStr;}newNum=nStr*1;}returnnewNum;}//加法Number.prototype.myAdd=function(arg2){returnoperationNumber(this,arg2,'+');}//减法Number.prototype.mySub=function(arg2){returnoperationNumber(this,arg2,'-');}//乘法Number.prototype.myMul=function(arg2){returnoperationNumber(this,arg2,'*');}//除法Number.prototype.myDiv=function(arg2){returnoperationNumber(this,arg2,'/');}
如果你发现了bug,评论区及时反馈,我及时跟进修复。
下载链接:网站源码/小程序源码/网站模板下载