I recently created a program that converts a binary number string to a decimal number. It seems to work fine until the string reaches around 15 or so digits in length and then it starts to be off by around 1 or 2 places (ie 1011010110100101001 returns 372008 instead of 372009). Once the string gets to around 20 digits it just returns NaN. Any ideas on whats happening?
function binaryConverter2(num) {
var numString = num.toString(),
total = 0,
i = numString.length-1,
j = 1;
while(i >= 0) {
total += (numString[i] *j);
j = j*2;
i --;
console.log(total);
}
return total;
}