This article is mainly about the floating point in JS operation of the problem is introduced, the need for friends can come to the reference, I hope to help you.
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); Get the result is: 6.8999999999999995 to the online search, a netizen said this is a JS floating-point arithmetic bugs, found a solution: method One: Have JS custom function code as follows: <script> //addition function, which is used to get exact addition result //Description: JavaScript additive result 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 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 is more convenient to call. Number.prototype.add = function (ARG) { return Accadd (arg,this); } //addition function, used to get 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 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: JavaScript division results will have errors, it will be more obvious 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. More convenient to call up. Number.prototype.div = function (ARG) { return Accdiv (this, arg); } //multiplication function to get accurate multiplication results & nbsp 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 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) } //Add a Mul method to the number type, which 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> Direct call function can be. Method Two: If you know the number of decimal places on the premise, you can consider by the floating point magnification to the integer (and then divided by the corresponding multiples), and then operation, so that the correct results can be obtained alert (11*22.9); Get 251.89999999999998 alert (11* (22.9*10)/10);/Get 251.9