The problem of JS arithmetic decimal point

Source: Internet
Author: User
Tags mul

Problem like this: 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 problem, directly with JS forget a result is: 206.08499999999998 how can this, Two numbers with only one decimal number multiply, how can the decimal point come out more so.

I Google a bit, found that this is a JavaScript floating point operation of a bug. For example: 7*0.8 JavaScript is calculated to be: 5.6000000000000005 on the internet to find some solutions, is to re-write some of the functions of floating-point operations. Here are some excerpts from these methods for the reference of friends who encounter the same problem:

Program code//Division function to get accurate division results

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.

Call: Accdiv (ARG1,ARG2)

Return value: Arg1 divided by the exact result of arg2

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 makes it easier to call.

Number.prototype.div = function (ARG) {

Return Accdiv (this, ARG);

}

multiplication function to get the exact multiplication result

Description: JavaScript multiplication results are error-evident when multiplying two floating-point numbers. 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 get 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 Precision results

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 makes it more convenient to call.

Number.prototype.add = function (ARG) {

Return Accadd (Arg,this);

}

Include these functions where you want them, and then call it to calculate. For example you want to calculate: 7*0.8, then change to (7). Mul (8) The other operations are similar, so that more accurate results can be obtained. --------------------------------------------------------------------------------------------------------------- ---------------------------------------------above is reproduced on the internet a JS cattle blog, but the above only mention the solution of addition, multiplication and division. At this time, many people will think, with addition, subtraction is not easy? I just almost let this idea suffer. The rest is not much to say, the code of the subtraction:

function Subtr (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 accuracy length

N= (R1>=R2) r1:r2;

Return ((arg1*m-arg2*m)/m). ToFixed (n); }

The problem of JS arithmetic decimal point

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.