Simple analysis of floating-point operation problem in JS _javascript skill

Source: Internet
Author: User
Tags mul pow

How does the floating-point type in JS operate?

For example: Var a=0.69;

I would like to get 6.9 direct so write Var c=a*10;

alert (c); The results are: 6.8999999999999995

A search on the internet, a netizen said this is a JS floating-point arithmetic bugs, found a solution:

Method One: Have JS custom function

Copy Code code as follows:

<script>

The addition function, which is used to get the exact addition result.
Description: JavaScript addition results will be error, in two floating-point number added when it is more obvious. This function returns a more precise 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
}
Adding an Add method to the number type is more convenient to call.
Number.prototype.add = function (ARG) {
Return Accadd (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 Accsub (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));
   //last Modify by Deeka
   //Dynamic Control precision length
    n= (r1>=r2)? r1:r2 ;
    return ((arg1*m-arg2*m)/m). ToFixed (n);
}

The

//division function, which is used to get the exact division result
//Description: The JavaScript division result is error, which is more noticeable when dividing two floating-point numbers. This function returns a more precise 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);
}
}
//Add a Div method to the number type, which is more convenient to invoke.
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);
}

var a=0.69;
var b=10;
alert (a*b);//6.8999999999999995
Alert ((a*100)/10);
</script>


Calling the function directly is OK.

method Two: 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

alert (11*22.9);//Get 251.89999999999998
Alert (11* (22.9*10)/10);//Get 251.9

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.