Problem with sum

Problem with sum

delph49delph49 Posts: 24Questions: 4Answers: 0

Hello,

I have a problem with a sum column. The result isn't exactly. I have 321.84999999999997 but it's 321.85.
I tried differents js:

1st solution:

  $(document).ready(function () {

                 var table = $('#summary').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 hours over this page
            thisPageTotal = api
                .column( 13, { page: 'current'} )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );

 // Update footer
            $( api.column( 13 ).footer() ).html(
                thisPageTotal + ' heure(s) sur cette page'
       {# ( sur '+ allPagesTotal +'  heure(s) totale(s))'#}
            );
        }   
});

2nd solution:

                $.fn.dataTable.Api.register('column().data().sum()', function () {
                                 return this.reduce(function (a, b) {
                                     var x = parseFloat(a) || 0;
                                     var y = parseFloat(b) || 0;
                                     return x + y;
                                 });
                             });

                 $(document).ready(function () {

                                     var table = $('#summary').DataTable({
                $('<button>Cliquez pour avoir la somme des coûts de M.O. de la sélection</button>')
                                        .prependTo('#summary')
                                        .on('click', function () {
                                            alert('Column sum is: ' + table.column(13).data().sum());
                                        });
                            });

3rd solution:

jQuery.fn.dataTable.Api.register('sum()', function ( ) {
                return this.flatten().reduce(function (a, b) {
                    if (typeof a === 'string') {
                        a = a.replace(/[^\d.-]/g, '') * 1;
                    }
                    if (typeof b === 'string') {
                        b = b.replace(/[^\d.-]/g, '') * 1;
                    }

                    return a + b;
                }, 0);
            });
$(document).ready(function () {

                                         var table = $('#summary').DataTable({
                    $('<button>Cliquez pour avoir la somme des coûts de M.O. de la sélection</button>')
                                            .prependTo('#summary')
                                            .on('click', function () {
                                                alert('Column sum is: ' + table.column(13).data().sum());
                                            });
                                });

An idea?

Answers

  • allanallan Posts: 61,438Questions: 1Answers: 10,052 Site admin

    Its for the same reason that 0.1 + 0.2 in Javascript doesn't === 0.3! See this web-site for a full explanation of floating point maths.

    Allan

  • delph49delph49 Posts: 24Questions: 4Answers: 0
    edited October 2018

    Thanks. I go see that!
    Edit: so if I understand the site, I can't have the exact sum?
    is it possible to round the numbers?

This discussion has been closed.