This inaccuracies result from the fact that many numbers do not have an exact representation in floating point, which is what JavaScript uses to store numbers.
A pragmatic solution is to round your result to a certain number of decimals after the decimal point, like so:
totalBetAmount = Math.round(totalBetAmount*100000000)/100000000;
Here is a snippet that shows the original number and the rounded number:
// Sample data
var data = {
Records: [
{ betAmount: 0.0001 },
{ betAmount: 0.0001 },
{ betAmount: 0.0001 },
],
RecordCount: 3
};
var totalBetAmount = 0;
for (i = 0; i < data.RecordCount; i++) {
totalBetAmount += parseFloat(data.Records[i].betAmount);
}
console.log('before:', totalBetAmount);
// round of inaccuracies, assuming decimals
totalBetAmount = Math.round(totalBetAmount*100000000)/100000000;
console.log('after:', totalBetAmount);