Reputation: 279
I have a data table and I have a footer which displays the sum of every column. Although the values above the footer are rounded to two decimal places, some of the values in the footer are not rounded to two decimal places.Screenshot My javascript for footercallback:
"footerCallback": function(row, data, start, end, display) {
var api = this.api();
api.columns('.sum', {
page: 'current'
}).every(function() {
var sum = this
.data()
.reduce(function(a, b) {
var x = parseFloat(a);
var y = parseFloat(b);
return x + y;
}, 0);
console.log(sum); //alert(sum);
$(this.footer()).html(sum);
});
}
Please help to resolve this.
Thanks
Upvotes: 2
Views: 3103
Reputation: 1
You can use toFixed(2);
For reference look here -Mozilla Docs
.reduce(function(a, b) {
var x = parseFloat(a);
var y = isNaN(parseFloat(b))?0:parseFloat(b);
let result=x + y;
return result.toFixed(2)
Upvotes: 3
Reputation: 878
You can use below function to only get two decimal digits
function (data, type, full) {
return parseFloat(data).toFixed(2);
}
Upvotes: 1