float number with intVal()

float number with intVal()

antoniocibantoniocib Posts: 277Questions: 62Answers: 1

Good morning,

I did not understand how it is possible that all these zeroes come out since a float variable has been declared on the server side, I am attaching the code.

footerCallback: function ( row, data, start, end, display ) {                                                                                                   
    var api = this.api(), data;

        var intVal = function ( i ) {
        return typeof i === 'string' ?
            i.replace(/[\$,]/g, '')*1 :
        typeof i === 'number' ?
        i : 0;
        };

                            total = api
                            .column( 1 )
                            .data()
                            .reduce( function (a, b) {
                                    return intVal(a) + intVal(b);
                            }, 0 );

                    // Total over this page
                    var rows = api.rows( {page: 'current'} ).indexes();
                    pageTotal = api
                            .cells( rows, 1 )
                            .data()
                            .reduce( function (a, b) {
                                    return intVal(a) + intVal(b);
                            }, 0 );

                    // Update footer
                    $( api.column( 1  ).footer() ).html(
                        ''+pageTotal +' ('+ total +'TOT)'
                    );
                },

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 3,000Questions: 87Answers: 421
    edited May 2022

    But you don't want an integer result?! Just drop intVal then. The way your individual numbers are fomatted this should work fine. In case you have thousand separators you might need to get rid of them though.

    The intVal function from this example: https://datatables.net/extensions/colreorder/examples/initialisation/footer_callback
    only removes the formatting from integer values like $ signs and the like. You are working with float variables that don't seem to have special formatting. That is not what intVal was made for.

    This should work:

    return a + b;
    
  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954
    edited May 2022 Answer ✓

    Thats more a Javascript issue than a Datatables issue. See this Floating Point technote for more info. You can use Javascript toFixed() to display only decimal points.

    Kevin

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1

    @rf1234

                                total = api
                                .column( 1 )
                                .data()
                                .reduce( function (a, b) {
                                        return a + b;
                                }, 0 );
    
                        // Total over this page
                        var rows = api.rows( {page: 'current'} ).indexes();
                        pageTotal = api
                                .cells( rows, 1 )
                                .data()
                                .reduce( function (a, b) {
                                        return a + b;
                                }, 0 );
    

    in this way?

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1

    @kthorngren in this way works correctly, thanks guys

    $( api.column( 1  ).footer() ).html(
    ''+pageTotal.toFixed(2) +' ('+ total.toFixed(2) +'TOT)'
    );
    
Sign In or Register to comment.