//加法
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;
}
//给Number类型增加一个add方法,调用起来更加方便
Number.prototype.add = function (arg) {
return accAdd(arg, this);
};
//乘法
function accMul(arg1,arg2)
{
var m=0,s1=arg1.toString(),s2=arg2.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类型增加一个mul方法,调用起来更加方便
Number.prototype.mul = function (arg) {
return accMul(arg, this);
};
//保留2位小数,如:2,会在2后面补上00.即2.00
function toDecimal(x) {
var f = parseFloat(x);
if (isNaN(f)) {
//非数字
return false;
}
var f = Math.round(x*100)/100;
var s = f.toString();
var rs = s.indexOf('.');
if (rs < 0) {
rs = s.length;
s += '.';
while (s.length <= rs + 2) {
s += '0';
}
return s;
}
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;
}
//给Number类型增加一个add方法,调用起来更加方便
Number.prototype.add = function (arg) {
return accAdd(arg, this);
};
//乘法
function accMul(arg1,arg2)
{
var m=0,s1=arg1.toString(),s2=arg2.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类型增加一个mul方法,调用起来更加方便
Number.prototype.mul = function (arg) {
return accMul(arg, this);
};
//保留2位小数,如:2,会在2后面补上00.即2.00
function toDecimal(x) {
var f = parseFloat(x);
if (isNaN(f)) {
//非数字
return false;
}
var f = Math.round(x*100)/100;
var s = f.toString();
var rs = s.indexOf('.');
if (rs < 0) {
rs = s.length;
s += '.';
while (s.length <= rs + 2) {
s += '0';
}
return s;
}