//加法
Number.prototype.add?=function(arg){
varr1,r2,m;
try{r1=this.toString().split(".")[1].length}catch(e){r1=0}
try{r2=arg.toString().split(".")[1].length}catch(e){r2=0}
m=Math.pow(10,Math.max(r1,r2))
return(this*m+arg*m)/m
}
//減法
Number.prototype.sub?=function(arg){
return?this.add(-arg);
}
//乘法
Number.prototype.mul?=function(arg)
{
varm=0,s1=this.toString(),s2=arg.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.div?=function(arg){
vart1=0,t2=0,r1,r2;
try{t1=this.toString().split(".")[1].length}catch(e){}
try{t2=arg.toString().split(".")[1].length}catch(e){}
with(Math){
r1=Number(this.toString().replace(".",""))
r2=Number(arg.toString().replace(".",""))
return(r1/r2)*pow(10,t2-t1);
}
}
測試
Js代碼
alert(Number(0.09999999).add(0.00000001));//彈出:?0.1
//注意盟戏,如果是負(fù)數(shù),一定要先使用Number轉(zhuǎn)型碉怔,否則結(jié)果不正確
alert(Number(-0.09999999).sub(0.00000001));//彈出:?-0.1
alert(Number(0.012345).mul(0.000001));//彈出:?1.2345e-8
alert(Number(0.000001).div(0.0001));//彈出:?0.01