In JavaScript, since floating point is defined with IEEE754,there will be some accuracy issues while doing floating point number arithmetic. Here is a function which can resolve the accuracy issue.
var calc = function(num1,operator,num2,len){
var numFixs = function(num){
var arr = num.toFixed(len).toString().split('.');
return parseInt(arr.join(''));
}
switch(operator){
case '+':
return ( numFixs(num1) + numFixs(num2) )/ Math.pow(10,len);
break;
case '-':
return ( numFixs(num1) - numFixs(num2) )/ Math.pow(10,len);
break;
case '*':
var tmp = ( numFixs(num1) * numFixs(num2) )/ Math.pow(10,len) / Math.pow(10,len);
return parseFloat(tmp.toFixed(len));
break;
case '/':
if (num2 == 0) {return 'Error'}
return ( numFixs(num1) / numFixs(num2) )/ Math.pow(10,len);
break;
}
}
//Examples
calc(2.01,'-',2.11 ,2);
calc(2.1,'-',2.111111 ,2);
calc(1,'-',2.111111 ,2);
calc(5,'-',2.111111 ,3);
If you have better idea. Please feel free to share with us.
Author : è‹æ´‹ Source : http://soulteary.com/2012/12/26/javascript-%E6%B5%AE%E7%82%B9%E6%95%B0%E7%9A%84%E7%B2%BE%E7%A1%AE%E8%AE%A1%E7%AE%97.html