1

How would I sum the total of amount that is returned in array by using Ajax?

Is there other way to calculate the sum with amount of .000 behind?

Example:

 for (i = 0; i < data.RecordCount; i++) {

 totalBetAmount += parseFloat(data.Records[i].betAmount);

Result: 0.22130000001

5
  • welcome to the magic world of floating point numbers ... where nothing is precise Commented Aug 27, 2016 at 8:20
  • Do you want to remove the decimal points? Commented Aug 27, 2016 at 8:20
  • 2
    Possible duplicate of How to deal with floating point number precision in JavaScript? Commented Aug 27, 2016 at 8:20
  • @MohitTanwani yes , correct i wish to move the decimal points but parseFloat seems not working Commented Aug 27, 2016 at 8:21
  • Try with ParseInt to get integer output. Commented Aug 27, 2016 at 8:21

5 Answers 5

1

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);

Sign up to request clarification or add additional context in comments.

Comments

0

Just round the decimal's:

Math.round();

2 Comments

No, This is not true! Can you give us an example how does your code work for this question?
Had you read the comments to the original question you'd understand what he actually meant =.=
0

Try like this.

//this will be total sum of all numbers
var totalSum = (data.Records || []).reduce(function(total, num){
    return parseFloat(total) + parseFloat(num);
});
//now you can do what you want using ParseInt or toFixed...

//convert to INT
totalSum = parseInt(totalSum);

//leave just 2 numbers after decimal like 1.12
totalSum = totalSum.toFixed(2);

Hope this helps.

Comments

0

If you use ES6 (ECMAScript 2015), you can use the reduce function (Great video tutorial to learn the reduce)

Here is a simple example:

var yourArray = [50, 60, 70, 100, 20]
var sum = yourArray.reduce((a, b) => a + b, 0);
console.log(sum); //Returns: 300

Comments

0

I'm not familiar with Ajax but this might be helpul :

var arr = [5.33655,6.6655,9.554];
var sum = 0;
for (var i = 0 ; i < arr.length ; i++) {
  sum += arr[i] 
};
console.log(sum.toFixed(3));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.