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, namely re-writing some floating point functions or directly expanding the multiples.
The following is an excerpt from these methods for your reference:
Copy codeThe Code is as follows:
// 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 );
}
// Subtraction function, used to obtain the exact subtraction result
// Note: the subtraction result of javascript has an error, which is obvious when two floating point numbers are added. This function returns a more precise subtraction result.
// Call: accSubtr (arg1, arg2)
// Return value: the exact result of arg1 minus arg2
Function accSubtr (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 ));
// Dynamically control the Precision Length
N = (r1> = r2 )? R1: r2;
Return (arg1 * m-arg2 * m)/m). toFixed (n );
}
// Add a subtr Method to the Number type to facilitate calling.
Number. prototype. subtr = function (arg ){
Return accSubtr (arg, this );
}
Include these functions where you want to use them, and then call them for calculation.
/If you know the number of decimal places, you can consider increasing the number of floating point numbers to an integer (and then dividing it by the corresponding multiples) Before performing operations, so that you can get the correct result.
Copy codeThe Code is as follows:
<Script>
Alert (11*(22.9*10)/10 );
</Script>