/*Solve the JS floating point (decimal) calculation subtraction Bug start****************************************//*The addition function, which is used to obtain the exact addition result * * Explanation: JavaScript addition result will have the error, when two floating point number adds the time to be more obvious. This function returns a more accurate addition result. * * Call: Accadd (ARG1,ARG2) * * return value: Arg1 plus arg2 exact result **/function Accadd (arg1, arg2) {If(IsNaN (ARG1)) {arg1 =0; }If(IsNaN (ARG2)) {arg2 =0; } arg1 =Number (ARG1); Arg2 =Number (ARG2);VarR1, R2, M, C;Try{r1 = arg1.tostring (). Split (".")[1].length; }Catch(e) {r1 =0; }Try{r2 = arg2.tostring (). Split (".")[1].length; }Catch(e) {r2 =0; } c = Math.Abs (R1-R2); m = Math.pow (10, Math.max (R1, r2));if (C >0) {var cm = Math.pow (10, c);if (R1 >R2) {arg1 = number (arg1.tostring (). Replace (".","")); arg2 = number (arg2.tostring (). Replace (".","")) *cm }Else{arg1 = number (arg1.tostring (). Replace (".","")) *cm arg2 = number (arg2.tostring (). Replace (".","")); } }Else{arg1 = number (arg1.tostring (). Replace (".","")); arg2 = number (arg2.tostring (). Replace (".","")); }Return (arg1 + arg2)/m;}//Adding an Add method to the number type makes it more convenient to call. Number.prototype.add =Function (ARG) {Return Accadd (This, arg);};/*Subtraction function, used to get the exact subtraction result * * Description: JavaScript subtraction results will be error, when the two floating-point subtraction will be more obvious. This function returns a more accurate subtraction result. * * Call: Accsub (ARG1,ARG2) * * return value: Arg1 plus arg2 exact result **/function Accsub (arg1, arg2) {If(IsNaN (ARG1)) {arg1 =0; }If(IsNaN (ARG2)) {arg2 =0; } arg1 =Number (ARG1); Arg2 =Number (ARG2);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 (Math.max (R1, r2));//Last modify by Deeka//Dynamic control accuracy Length n = (r1 >= r2)?R1:R2;Return ((ARG1 * M-ARG2 * m)/m). toFixed (n);}//Adding a Mul method to the number type makes it more convenient to call. Number.prototype.sub =Function (ARG) {Return Accsub (This, arg);};/*multiplication function, which is used to get the exact multiplication result * * Description: JavaScript multiplication results will be error, when the two floating point multiplied by the more obvious. This function returns a more accurate multiplication result. * * Call: Accmul (ARG1,ARG2) * * return value: Arg1 times the exact result of ARG2 **/function Accmul (arg1, arg2) {If(IsNaN (ARG1)) {arg1 =0; }If(IsNaN (ARG2)) {arg2 =0; } arg1 =Number (ARG1); Arg2 =Number (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 makes it more convenient to call. Number.prototype.mul =Function (ARG) {return Accmul (this, arg);}; /** * * Division function, used to get accurate division results * * Description: JavaScript division results will be error, when the two floating point number is more obvious when dividing. This function returns a more accurate division result. * * Call: Accdiv (ARG1,ARG2) * * return value: Arg1 divided by arg2 exact result **/function accdiv (arg1, arg2) {if (IsNaN (arg1)) {arg1 = 0;} if (IsNaN (arg 2) {arg2 = 0;} arg1 = number (ARG1); arg2 = number (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 (T2-T1); }}//adds a Div method to the number type, which is more convenient to call. Number.prototype.div = function (ARG) {return Accdiv (this, arg);};/ Solve the JS floating point (decimal) calculation subtraction bug end****************************************/
Solve the JS floating point (decimal) calculation subtraction bug