Subtotals number precision
Subtotals number precision
Robayb
Posts: 9Questions: 0Answers: 0
I'm using the subtotal script provided in the documentation. It works great on all columns but one, which has 11 digits past the decimal. ( $6807.330000000001) The data seems the same as on columns that display properly. I don't have any blank cells or unusual characters. Is there a way to define this as money?
This is the code:
$('#COGtable').DataTable( {
"footerCallback": function ( row, data, start, end, display ) {
var api = this.api(), data;
// Remove the formatting to get integer data for summation
var intVal = function ( i ) {
return typeof i === 'string' ?
i.replace(/[\$,]/g, '')*1 :
typeof i === 'number' ?
i : 0;
};
// Total over all pages
total = api
.column( 17 )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
// Update footer
$( api.column( 17 ).footer() ).html('$'+total );
total = api
.column( 5 )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
$( api.column( 5 ).footer() ).html('$'+total );
}
} );
This discussion has been closed.
Replies
I want to ask again if anyone can help on this.
I'm trying to get subtotals in the footer using the code below. When it's sorted Decending , then I get a subtotal like this $6807.330000000001. But as soon as I sort ASC then it will be correct : $6807.33.
Here is snippet from this site documentation:
total = api
.column( 37 )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
$( api.column( 37 ).footer() ).html('$'+total );
I'm sure there's other ways of doing this but I format the values using javaScript.
total is the column total, and I'm formatting with zero decimals, use 2 for 2 places.
here's what is really looks like in the footerCallback
here's the floatVal function also,