Recently in a payroll accounting system, all operations in the foreground, so the use of JS to do.
After the finish, by hand accounting, found a strange problem. Is the result of JS calculated with the calculator calculated results There is a gap.
Thought for a long time, also did not think out where the problem.
The problem is this:
37.5*5.5=206.08 (JS figure out is such a result, I rounded up to take two decimal places)
I first suspect that the problem is rounding, just use JS to forget a result: 206.08499999999998
How can this, two only one decimal number multiplication, how can more out of this decimal point out.
I Google a bit, found that this is a JavaScript floating point operation of a bug.
For example: 7*0.8 JavaScript is calculated as: 5.6000000000000005
Some solutions have been found online, which is to re-write some functions of floating-point arithmetic.
Here are some excerpts from these methods for the reference of friends who encounter the same problem:
Program code
The Division function, which is used 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) {}
Returnnumber (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);
}
An addition function that is used to obtain accurate addition results
Note: 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
}
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)
Similar to other operations, you can get more accurate results.
--------------------------------------------------------------------------------------------------------------- ---------------------------------------------
The above is reproduced on the website of a JS cattle blog, but the above only mentions 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,
Code that posts 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);
}
--------------------------------------------------------------------------------------------------
JS reserved 2 decimal places (mandatory)
function Changetwodecimal_f (x)
{
var f_x = parsefloat (x);
if (IsNaN (f_x))
{
Alert (' Function:changetwodecimal->parametererror ');
return false;
}
var f_x = Math.Round (x*100)/100;
var s_x = f_x.tostring ();
var pos_decimal = S_x.indexof ('. ');
if (Pos_decimal < 0)
{
Pos_decimal = S_x.length;
S_x + = '. ';
}
while (s_x.length <= Pos_decimal + 2)
{
s_x + = ' 0 ';
}
return s_x;
}
Usage:
Pass in a folat point, of course, from the page to get a string, you have to convert the next.
Such as:
var value=document.getelementbyidx_x ("Textname"). Value;
var temp=parsefloat (value);
Changetwodecimal_f (temp);
You can have it.
Of course you can do it in a Java program ... Use
Java.text.DecimalFormat Dec = Newjava.text.DecimalFormat ("0.00");
Dec.format (data);
You can keep the floating-point number only two bits ....
Excerpt from: http://blog.sina.com.cn/s/blog_53edf7c10100wlcw.html
Solution of float Accuracy in JS