How is the floating -point type in JS?
For example: var a = 0.69;
I want to get 6.9 directly to write VAR C = A*10;
alert (c); the result is: 6.899999999999995
When you search online, some netizens said that this is a JS floating -point calculation bug, and found a solution:
Method 1: JS custom function
Copy code code as follows:
<script>
// 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);
}
// 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 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 accuracy length
n = (R1> = R2)? R1: R2;
Return ((ARG1*M-RG2*M)/m) .tofixed (n);
}
// 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);
}
var a = 0.69;
var b = 10;
alert (a*b); // 6.89999999999995
alert ((A*100)/10);
</script>
Just call the function directly.
Method 2: If you know the number of decimal digits, you can consider boring the number of floating -point numbers to the integer (finally divide the corresponding multiple), and then perform the operation operation. This can get the correct result.
Alert (11*22.9); // Get 251.8999999999998
Alert (11*(22.9*10)/10); // get 251.9