Subtraction of JS Control precision

Source: Internet
Author: User
Tags mul pow

//addition function        functionAccadd (arg1, arg2) {varR1, 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; }        //Add an Add method to the number type, which is used directly with. Add to complete the calculation. Number.prototype.add =function(ARG) {returnAccadd (ARG, This);        }; //Subtraction Function        functionsubtr (arg1, 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 (10, Math.max (R1, r2)); //Last modify by Deeka            //Dynamic Control accuracy lengthn = (r1 >= r2)?R1:R2; return((ARG1 * M-ARG2 * m)/m). toFixed (n); }        //add an Add method to the number type, and use the. Sub directly to complete the calculation. Number.prototype.sub =function(ARG) {returnSUBTR ( This, ARG);        }; //multiplication Function        functionAccmul (arg1, arg2) {varm = 0, S1 = arg1.tostring (), S2 =arg2.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); }        //Add a Mul method to the number type and use it directly with the. Mul to complete the calculation. Number.prototype.mul =function(ARG) {returnAccmul (ARG, This);        }; //Division function        functionAccdiv (arg1, arg2) {varT1 = 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); }        }        //Add a div method to the number type, and use the. Div directly to complete the calculation. Number.prototype.div =function(ARG) {returnAccdiv ( This, ARG);        }; functionsetnumandweight (num, weight, index, obj) {$ (obj). Parent (). Next ("TD"). Text (Getfloatbynum (weight, 4)); varHTML = "Number of pieces:" + num + "<br>" + "Weight:" + getfloatbynum (weight, 4); $("tr.tr-" + Index). Children (). EQ (4). HTML (HTML); }        //the mandatory system retains 4 decimal places, and the 5th place goes directly. such as: 2, will be 2 after the 00. That is, 2.00        functionToDecimal4 (x) {varf =parsefloat (x); if(IsNaN (f)) {return false; }            varf = Math.floor (x * 10000)/10000; vars =f.tostring (); varrs = S.indexof ('. ')); if(Rs < 0) {RS=s.length; S+ = '. '; }             while(s.length <= RS + 4) {s+ = ' 0 '; }            returns; }

Call, directly with:

Weight = parsefloat (perweight). Mul (parseint (num). Add (Onboardnum)). Sub (onboardweight);

Subtraction of JS Control precision

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.