jquery Numerical accuracy calculation

Source: Internet
Author: User

         /** * Determine if the values are equal * @param source Original comparison data * @param target target comparison data * @param accuracy precision Value * @returns {Boolean}*/isequal:function(source,target,accuracy) {accuracy= Accuracy = = undefined? 10:accuracy;//default Precision bit            returnparsefloat (source). toFixed (accuracy) = = =parsefloat (target) toFixed (accuracy); }        /** * Add value * @param firstnumber * @param lastnumber * @returns {number}*/Accuracyadd:function(firstnumber,lastnumber) {varnum1,num2,m; Try{num1=firstnumber.tostring (). Split (".") [1].length}Catch(e) {num1=0}; Try{num2=lastnumber.tostring (). Split (".") [1].length}Catch(e) {num2=0}; M=math.pow (10, Math.max (num1,num2)); return(firstnumber*m+lastnumber*m)/m;        }        /** * Numeric subtraction * @param firstnumber * @param lastnumber * @returns {string}*/accuracysub:function(firstnumber,lastnumber) {varR1,r2,m,n; Try{r1=firstnumber.tostring (). Split (".") [1].length}Catch(e) {r1=0}        Try{r2=lastnumber.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((firstnumber*m-lastnumber*m)/m). ToFixed (n);    }        /** division function, divide two number * @param arg1 * @param arg2 * @returns {#}*/Accuracydiv:function(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 (10,T2-T1);        }    }        /** multiplication function, multiply by two * @param arg1 * @param arg2 * @returns {number}*/Accuracymul:function(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)}

jquery Numerical accuracy calculation

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.