JS precise Multiplication method

Source: Internet
Author: User
Tags mul

 JavaScript (JS) Decimal multiplication Division problemCategory: Javascript2012-07-24 10:09 2851 People read comments (0) favorite reports Javascriptfunctionmath Test div One, use JS to calculate 12.32 * 7   results? Answer:86.24000000000001  Why does this problem occur? How to solve? JS has a bug when dealing with the multiplication method of decimals, which can be solved by turning decimals into integers. The above calculation can be changed to: 12.32 * 100 * 7/100 The result is: 86.24, correct.   Another calculation: 8.80 * 100 * 12/100 results: Similar problems can occur with 105.6000000000000238.80.   accuracy increased by 10 times times: 8.80 * 1000 * 12/1000 results: 105.6 normal.  16.40 * 1000000 * 6/1000000 The result also has the question   in order to let the JS execution more accurate, in later JS decimal calculation directly expands the value 10,000 times times, then divides the 10000, can solve the problem. var num = 38.80;var num2 = 13;alert (num * 10000 * 12/10000);  multiply and divide this number after testing 10000 most suitable, small some numbers out of question, big (1000000) some numbers also problem. Second, <script defer>number.prototype.rate=function () {varostr=this.tostring (); if (Ostr.indexof (".") ==-1) Return1;elsereturnmath.pow (10,parseint (Ostr.length-ostr.indexof (".") -1));}  functiontran () {args=tran.arguments;vartemp=1;for (i=0;i<args.length;i++) temp*=args[i]*args[i].rate (); for (i=0;i<args.length;i++) temp/=args[i].rate (); Returntemp} alert (Tran (11,22.9)); </script>   The solution is a troublesome approach, but it gives you an idea of the actual process of solving the problem.  //division function, which is used to get the exact division result//Description: JaThe division result of Vascript will be error, it will be more obvious when the two floating-point numbers divide. This function returns a more accurate division result. Call: Accdiv (ARG1,ARG2)//return value: Arg1 divided by arg2 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);}  //adds a Div method to the number type, which is more convenient to call. Number.prototype.div = function (ARG) {return Accdiv (this, arg);}  //multiplication function, which is used to get the exact multiplication result//description: JavaScript multiplication result will have error, it will be more obvious when two floating-point numbers multiply. 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) {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 (ARG, this);} The  //addition function is used to obtain the exact addition//description: 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. 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} //adds an Add method to the number type, More convenient to call. Number.prototype.add = function (ARG) {return accadd (arg,this);}

JS Exact multiplication method

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.