Subtotals number precision

Subtotals number precision

RobaybRobayb Posts: 9Questions: 0Answers: 0
edited December 2015 in Free community support

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 );

}
} );

Replies

  • RobaybRobayb Posts: 9Questions: 0Answers: 0
    edited January 2016

    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 );

  • glendersonglenderson Posts: 231Questions: 11Answers: 29

    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.

    $jq(api.column(" + columnCounter + ").footer()).html(total.formatCurrency(0));"
    
    Number.prototype.formatCurrency = function (c, d, t) {
        var n = this,
            c = isNaN(c = Math.abs(c)) ? 2 : c,
            d = d == undefined ? "." : d,
            t = t == undefined ? "," : t,
            s = n < 0 ? "-" : "",
            i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "",
            j = (j = i.length) > 3 ? j % 3 : 0;
        return s + "$" + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
    };
    

    here's what is really looks like in the footerCallback

      , "footerCallback": function(tfoot, data, start, end, display) {
          var api=this.api();
          if (api.column(6, {search:"applied"}).data().length) {var total=api.column(6, {search:"applied"}).data().reduce( function (a,b) { return floatVal(a) + floatVal(b); }, 0);} else {var total=0;};$jq(api.column(6).footer()).html(total.toFixed(2));
          if (api.column(7, {search:"applied"}).data().length) {var total=api.column(7, {search:"applied"}).data().reduce( function (a,b) { return floatVal(a) + floatVal(b); }, 0);} else {var total=0;};$jq(api.column(7).footer()).html(total.formatCurrency(0));}
    

    here's the floatVal function also,

    var floatVal = function (i) {
        if (typeof i === "number") {
            return i;
        } else if (typeof i === "string") {
            i = i.replace("$", "")
            i = i.replace(",", "")
            var result = parseFloat(i);
            if (isNaN(result)) {
                try {
                    var result = $jq(i).text();
                    result = parseFloat(result);
                    if (isNaN(result)) { result = 0 };
                    return result * 1;
                } catch (error) {
                    return 0;
                }
            } else {
                return result * 1;
            }
        } else {
            alert("Unhandled type for totals [" + (typeof i) + "]");
            return 0
        }
    };
    
This discussion has been closed.