Solve the error problem of JavaScript subtraction and tofixed

Source: Internet
Author: User
Tags mul pow

1     //to replace the original tofixed to solve the accuracy error problem2Number.prototype.mytofixed=function(s) {3       if(s = =NULL) {s = 0;}4       varValue = Math.pow (10,s);//Math.pow (x, y) returns the value of the Y-power of X, which is the value of the 10 len-squared5       //JavaScript's subtraction result will be error, using a custom method6       varReturnstr = Math.Round ( This. Mul (value)). div (value). toString ();7       //var returnstr= (parseint (This * value + 0.5)/value). toString ();8       varPointIndex = Returnstr.indexof ("."));9       if(PointIndex < 0 && s > 0) {//If there is no decimal point, add a decimal point and then 0TenReturnstr = Returnstr + "."; One          for(i = 0; i < s; i++){ AReturnstr = Returnstr + "0"; -         } -}Else { the         varWeishu = returnstr.length-1-PointIndex; -          for(i = 0; i < (S-weishu); i++) {//if there are decimal points, directly fill the missing 0 -Returnstr = Returnstr + "0"; -         } +       } -       returnReturnstr; +}
1     //multiplication function to get the exact multiplication result2     //Description: JavaScript multiplication results are error-evident when multiplying two floating-point numbers. This function returns a more accurate multiplication result. 3     //Call: Accmul (ARG1,ARG2)4     //return value: arg1 times the exact result of Arg25     functionAccmul (arg1, arg2) {6         varm = 0, S1 = arg1.tostring (), S2 =arg2.tostring ();7         Try{m + = S1.split (".") [1].length}Catch(e) {}8         Try{m + = S2.split (".") [1].length}Catch(e) {}9         returnNumber (S1.replace (".", "")) * Number (S2.replace (".", ""))/Math.pow (10, M)Ten     } One     //Usage: A     //Adding a Mul method to the number type makes it more convenient to call.  -Number.prototype.mul =function(ARG) { -         returnAccmul ( This, ARG); the}
1     //The division function, which is used to get accurate division results.2     //Description: The result of the division of JavaScript will be error, which will be obvious when dividing two floating-point numbers. This function returns a more accurate division result. 3     //Call: Accdiv (ARG1,ARG2)4     //return value: Arg1 divided by the exact result of Arg25     functionAccdiv (arg1, arg2) {6         varT1 = 0, t2 = 0, R1, R2;7         Try{T1 = arg1.tostring (). Split (".") [1].length}Catch(e) {}8         Try{t2 = arg2.tostring (). Split (".") [1].length}Catch(e) {}9          with(Math) {TenR1 = number (arg1.tostring (). Replace (".", "")) Oner2 = number (arg2.tostring (). Replace (".", "")) A             return(R1/R2) * POW (T2-t1); -         } -     } the     //Usage: -     //Adding a Div method to the number type makes it easier to call.  -Number.prototype.div =function(ARG) { -         returnAccdiv ( This, ARG); +}
1     //An addition function that is used to obtain accurate addition results2     //Note: The addition of JavaScript will have an error, and it will be more obvious when the two floating-point numbers are added. This function returns a more accurate addition result. 3     //Call: Accadd (ARG1,ARG2)4     //return value: Arg1 plus arg2 's exact result5     functionAccadd (arg1, arg2) {6         varR1, R2, M;7         Try{r1 = arg1.tostring (). Split (".") [1].length}Catch(e) {r1 = 0 }8         Try{r2 = arg2.tostring (). Split (".") [1].length}Catch(e) {r2 = 0 }9m = Math.pow (10, Math.max (R1, r2))Ten         return(ARG1 * m + arg2 * m)/m One     } A     //Usage: -     //Adding an Add method to the number type makes it more convenient to call.  -Number.prototype.add =function(ARG) { the         returnAccadd (ARG, This); -}
1     //subtraction function, which is used to get the exact subtraction result2     functionsubtr (arg1, arg2) {3         varR1, R2, M, N;4         Try{r1 = arg1.tostring (). Split (".") [1].length}Catch(e) {r1 = 0 }5         Try{r2 = arg2.tostring (). Split (".") [1].length}Catch(e) {r2 = 0 }6m = Math.pow (10, Math.max (R1, r2));7         //Last modify by Deeka8         //Dynamic Control accuracy length9n = (r1 >= r2)?r1:r2;Ten         return((ARG1 * M-ARG2 * m)/m). ToFixed (n);//we used tofixed here. One}

Calculate as little as possible in the page with JS calculation, as far as possible in the background calculation, really want to use JS and do not want to have errors, you can the above method.

Solve the error problem of JavaScript subtraction and tofixed

Related Article

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.