Skip to content

Instantly share code, notes, and snippets.

@ziluo
Last active August 29, 2015 14:03
Show Gist options
  • Save ziluo/38076f1ef6a20c51aa94 to your computer and use it in GitHub Desktop.
Save ziluo/38076f1ef6a20c51aa94 to your computer and use it in GitHub Desktop.
数字运算
//加法
Number.prototype.add = function(arg){
var r1,r2,m,n;
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));
n=(r1>=r2)?r1:r2;
return ((this*m+arg*m)/m).toFixed(n);
}
//减法
Number.prototype.sub = function (arg){
   var r1,r2,m,n;
   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));
   n=(r1>=r2)?r1:r2;
   return ((this*m-arg*m)/m).toFixed(n);
}
//乘法
Number.prototype.mul = function (arg)
{
var m=0,s1=this.toString(),s2=arg.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)
}
//除法
Number.prototype.div = function (arg){
var t1=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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment