Today's Question:  What are you most afraid of as a programmer?

# Accurate floating point number calculation in JavaScript

sonic0002      2012-12-27 11:07:49      4,224    0    0

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.