Floating -point errors occur when the multiplication operation of the decimal multiplication in JS. You can test it for details:
<script>
Alert (11*22.9)
</script>
The result was 251.8999999999998 instead of 251.9
There must be many people's headaches. So how to solve it? Here, the solution is given.
1,
Copy code code as follows:
<script>
Alert (11*(22.9*10)/10);
</script>
The approximate idea of solving the problem is that the factor is enlarged into an integer first, and finally divided by the corresponding multiple, so that the correct result can be obtained.
2,
Copy code code as follows:
<script defer>
Number.prototype.raate = Function () {
var os.tostring ();
if (OSTR.INDEXOF (".") ==-1)
Return 1;
else
Return math.Pow (10, PARSEINT (osr.length-organ.indexof (".");
}
function trans () {
args = trans.arguments;
var test = 1;
for (i = 0; I <args.length; i ++)
temp*= args [i]*args [i] .raate ();
for (i = 0; I <args.length; i ++)
temp/= args [i] .raate ();
Return Temp
}
Alert (trans (11,22.9));
</script>
This solution is a more troublesome method, but it can make you probably understand the actual process of solving this problem.
You can also use the method of four houses and five incoming methods. You can use math.round in JS to achieve the four houses and five entry of the integer. If you need to achieve accurate to decimal points, you need to write a function.
Copy code code as follows:
Function Fordight (DIGHT, How) {
DIGHT = Math.round (DIGHT*Math.pow (10, HOW))/Math.pow (10, HOW);
Return dign;
}
// Except the method to get the precise method of removing the method
// Description: The results of JavaScript will have an error, and it will be obvious when the two floating -point numbers are removed. This function returns a more accurate removal result.
// Call: ACCDIV (ARG1, ARG2)
// Return value: ARG1 Except the accurate results 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);
}
}
// Add a div method to Number type, which is more convenient to call.
Number.prototype.div = Function (ARG) {
Return accdiv (this, arg);
}
// multiplication function to obtain accurate multiplication results
// Description: The results of the JavaScript multiplication results will have an error, and it will be obvious when the two floating -point numbers are multiplied. This function returns a more accurate multiplication result.
// Call: ACCMul (ARG1, ARG2)
// Return value: ARG1 is multiplied by ARG2's accurate results
Function accmul (ARG1, ARG2)
{{
varm 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 Number type, which is more convenient to call.
Number.prototype.mul = Function (ARG) {
Return accmul (arg, this);
}
// Add method to obtain accurate additional results
// Description: The results 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 result of addition.
// Call: Accidd (ARG1, ARG2)
// Return value: ARG1 plus the precise results of ARG2
Function acadd (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 Number type, which is more convenient to call.
Number.prototype.add = Function (ARG) {
Return acadd (arg, this);
}