Currently, I am facing an issue while using a filter to calculate totals from dynamic data in ng-repeat. The problem lies in my inability to limit the decimals to 2 places. Below is the code snippet of my filter:
app.filter('sumByKey', function() {
return function (data, key) {
if (typeof (data) === 'undefined' || typeof (key) === 'undefined') {
return 0;
}
var sum = 0;
for (var i = data.length - 1; i >= 0; i--) {
//sum += parseFloat(data[i][key]);
sum += Math.round((data[i][key]) * 1e12) / 1e12;
}
return sum;
};
});
The use of "parseFloat" did not yield the expected results, and even attempting to employ "toFixed(2)" led to strange and unreadable outcomes. Despite testing this in both IE and Chrome, the issue persists as shown below:
https://i.sstatic.net/RqiMu.png
Please note that this question with regards to "toFixed(2)" is distinct from other answers, as utilizing "toFixed(2)" generates lengthy and incomprehensible numbers.
Your help and guidance on this matter are highly valued!