How to get average to work with numbers that have a comma
How to get average to work with numbers that have a comma
![smason](https://secure.gravatar.com/avatar/188da6f53d7a2ec1aa5efe1d1c0cd5e8/?default=https%3A%2F%2Fvanillicon.com%2F188da6f53d7a2ec1aa5efe1d1c0cd5e8_200.png&rating=g&size=120)
Consider the following snippet inside a footer callback.
"footerCallback": function(row, data, start, end, display) {
var api = this.api();
api.columns('.avg', {
page: 'current'
}).every(function() {
var avg = this.data().average();
avg = avg.toFixed(2);
$(this.footer()).html("" + avg + "%");
});
It seems to return NaN if a value in one of the cells it's looking at contains a comma. How do I remove the comma so that the average function works correctly?
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
You could use the
replace
function similar to this example:https://datatables.net/examples/advanced_init/footer_callback.html
Kevin
I modified the intVal function to my needs, just to remove the comma.
Calculating the average was a little tricky because you have to get the length of the dataset as the denominator. Maybe this could help someone else too.