Copy Code code as follows:
The Division function, which is used to get the exact division result
Description: JavaScript division results will be error, the two floating-point numbers are more obvious when dividing. This function returns a more precise division result.
Call: Accdiv (ARG1,ARG2)
Return value: Arg1 divided by Arg2 's exact result
function Accdiv (ARG1,ARG2) {
var t1=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) *pow (10,T2-T1);
}
}
Adding a Div method to the number type is more convenient to call.
Number.prototype.div = function (ARG) {
Return Accdiv (this, ARG);
};
multiplication function to get the exact result of the multiplication
Description: JavaScript multiplication results will be error, in two floating-point numbers are more obvious when multiplying. This function returns a more accurate result of the multiplication.
Call: Accmul (ARG1,ARG2)
return value: Arg1 times Arg2 's exact result
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);
}
Adding a Mul method to the number type is more convenient to call.
Number.prototype.mul = function (ARG) {
Return Accmul (ARG, this);
};
The addition function, which is used to get the exact addition result.
Description: JavaScript addition results will be error, in two floating-point number added when it is more obvious. This function returns a more precise addition result.
Call: Accadd (ARG1,ARG2)
return value: Arg1 plus arg2 's exact result
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;
}
Adding an Add method to the number type is more convenient to call.
Number.prototype.add = function (ARG) {
Return Accadd (Arg,this);
};
Subtraction function
function Accsub (ARG1,ARG2) {
var r1,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));
Last modify by Deeka
Dynamic Control Precision Length
N= (R1>=R2) r1:r2;
Return ((arg2*m-arg1*m)/m). ToFixed (n);
}
Adding a sub method to the number class makes it easier to invoke
Number.prototype.sub = function (ARG) {
Return Accsub (Arg,this);
};