How to solve the problem of the error in JS floating-point arithmetic _javascript skill

Source: Internet
Author: User
Tags arithmetic mul pow

37.5*5.5=206.08 (JS figure out is such a result, I rounded to take two decimal places)
I first suspect is rounding the problem, directly with JS calculate a result is: 206.08499999999998
How can this, two numbers only one decimal number multiplied, how can more such a decimal point out.
I googled it and found that it was a bug in JavaScript floating-point arithmetic.
For example: 7*0.8 JavaScript figure out is: 5.6000000000000005

Some solutions have been found on the Internet, that is, to write down the functions of floating-point operations or to enlarge the multiplication directly.
Here's an excerpt of these methods for a friend who encounters the same problem:

Copy Code code as follows:

The Division function, which is used to get the exact division result
Description: JavaScript division results will be error, the two floating-point numbers are more obvious when dividing. This function returns a more precise division result.
Call: Accdiv (ARG1,ARG2)
Return value: Arg1 divided by Arg2 's 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);
}
}
Adding a Div method to the number type is more convenient to call.
Number.prototype.div = function (ARG) {
Return Accdiv (this, ARG);
}

multiplication function to get the exact result of the multiplication
Description: JavaScript multiplication results will be error, in two floating-point numbers are more obvious when multiplying. This function returns a more accurate result of the multiplication.
Call: Accmul (ARG1,ARG2)
return value: Arg1 times Arg2 's exact result
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 is more convenient to call.
Number.prototype.mul = function (ARG) {
Return Accmul (ARG, this);
}

The

//addition function, which is used to get the exact addition result
//Description: The addition of JavaScript will have error, it will be more obvious when two floating-point numbers are added. This function returns a more precise addition result.
//Call: Accadd (ARG1,ARG2)
//return value: Arg1 plus arg2 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)//
}
// Adding an Add method to the number type is more convenient to call.
Number.prototype.add = function (arg) {
return accadd (arg,this);
}

Subtraction function, which is used to obtain accurate subtraction results.
Description: JavaScript subtraction results will be error, in two floating-point numbers added when it is more obvious. This function returns a more accurate subtraction result.
Call: ACCSUBTR (ARG1,ARG2)
return value: Arg1 minus arg2 's exact result
function Accsubtr (ARG1,ARG2) {
var r1,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));
Dynamic Control Precision Length
N= (R1>=R2) r1:r2;
Return ((arg1*m-arg2*m)/m). ToFixed (n);
}
Adding a SUBTR method to the number type is more convenient to call.
NUMBER.PROTOTYPE.SUBTR = function (ARG) {
Return ACCSUBTR (Arg,this);
}

It is OK to include these functions in the place you want to use and then call it to compute.

/If you know the number of decimal places on the premise, you can consider by the floating point magnification to the integral type (and then divided by the corresponding multiples), and then the operation, so that the correct results can be

Copy Code code as follows:

<script>
Alert (11* (22.9*10)/10);
</script>

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.