The JS budget Accuracy Problem is really troublesome. This can solve some problems, although there is a bug. The function code is as follows:
The Code is as follows:
/*
* Arithmetic precision correction function
* M value 1 (number)
* N value 2 (number)
* Op operator (string)
*/
Function fixMath (m, n, op ){
Var a = (m + "");
Var B = (n + "");
Var x = 1;
Var y = 1;
Var c = 1;
If (a. indexOf (".")> 0 ){
X = Math. pow (10, a. length-a. indexOf (".")-1 );
}
If (B. indexOf (".")> 0 ){
Y = Math. pow (10, B. length-B. indexOf (".")-1 );
}
Switch (op)
{
Case '+ ':
Case '-':
C = Math. max (x, y );
M = Math. round (m * c );
N = Math. round (n * c );
Break;
Case '*':
C = x * y
M = Math. round (m * x );
N = Math. round (n * y );
Break;
Case '/':
C = Math. max (x, y );
M = Math. round (m * c );
N = Math. round (n * c );
C = 1;
Break;
}
Return eval ("(" + m + op + n + ")/" + c );
}
Function usage:
The Code is as follows:
FixMath (2.3, 1.9 ,'*')
FixMath (1.98, 1.9 ,'-')
FixMath (83.50, 74.15 ,'-')