0

I have an array of data that I need to add up cumulatively, but the data becomes something else now that it only has two decimal places. Before, it was working just fine, but some numbers had over six decimal places. Below, I have posted some JS code with the arrays as examples. How can I cumulatively add the numbers in my array without them getting messed up?

JS

// Cumulative code
var cumulative = (cum) => {
  var val = 0;
  return cum.map(c => val += c);
};

var cumSum = data.map(cumulative);

// The data array that is used above...
var data = [126686.27, 125910.27, 128302.27, 126702.27, 114276.27, 111852.27, 124657.00, 124657.00, 124657.00, 124657.00, 124657.00, 124657.00]

// The incorrect result from mapping it to the cumulative variable...
[0126686.27, 0126686.27125910 .27, 0126686.27125910 .27128302 .27, 0126686.27125910 .27128302 .27126702 .27, 0126686.27125910 .27128302 .27126702 .27114276 .27, 0126686.27125910 .27128302 .27126702 .27114276 .27111852 .27, 0126686.27125910 .27128302 .27126702 .27114276 .27111852 .27124657 .00, 0126686.27125910 .27128302 .27126702 .27114276 .27111852 .27124657 .00124657 .00, 0126686.27125910 .27128302 .27126702 .27114276 .27111852 .27124657 .00124657 .00124657 .00, 0126686.27125910 .27128302 .27126702 .27114276 .27111852 .27124657 .00124657 .00124657 .00124657 .00, 0126686.27125910 .27128302 .27126702 .27114276 .27111852 .27124657 .00124657 .00124657 .00124657 .00124657 .00, 0126686.27125910 .27128302 .27126702 .27114276 .27111852 .27124657 .00124657 .00124657 .00124657 .00124657 .00124657 .00]
Aalexander
  • 4,987
  • 3
  • 11
  • 34
grahamfk45c
  • 83
  • 1
  • 9

0 Answers0