The problem is as follows:
37.5*5.5 = 206.08 (JS calculates this result. I rounded it to two decimal places)
I first suspected it was a rounding problem, and I used js to calculate a result: 206.08499999999998.
How can this happen? How can we multiply two digits with only one decimal number.
I Googled and found that this was a bug in Javascript floating point operations.
For example, 7*0.8 Javascript is calculated as follows: 5.6000000000000005
Some solutions have been found on the Internet, that is, some floating point operation functions have been re-written.
The following is an excerpt from these methods for your reference:
Program code
// Division function, used to obtain accurate division results
// Note: the division result of JavaScript has an error, which is obvious when two floating point numbers are separated. This function returns a more precise division result.
// Call: accdiv (arg1, arg2)
// Return value: the exact result of dividing arg1 by 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 );
}
}
// Add a div Method to the number type to facilitate calling.
Number. Prototype. DIV = function (ARG ){
Return accdiv (this, ARG );
}
// Multiplication function, used to obtain accurate multiplication results
// Note: there is an error in the Javascript multiplication result, which is obvious when two floating point numbers are multiplied. This function returns a more accurate multiplication result.
// Call: accmul (arg1, arg2)
// Return value: the exact result of multiplying arg1 by 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)
}
// Add a mul Method to the number type to facilitate calling.
Number. Prototype. Mul = function (ARG ){
Return accmul (ARG, this );
}
// Addition function, used to obtain accurate addition results
// Note: The addition result of JavaScript has an error, which is obvious when two floating point numbers are added. This function returns a more accurate addition result.
// Call: accadd (arg1, arg2)
// Return value: the exact result of adding arg2 to arg1
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
}
// Add an add method to the number type to facilitate calling.
Number. Prototype. Add = function (ARG ){
Return accadd (ARG, this );
}
Include these functions where you want to use them, and then call them for calculation.
For example, if you want to calculate 7*0.8, change it to (7). Mul (8)
Similar to other operations, you can obtain more accurate results.