Change Format of Sum within Footer

Change Format of Sum within Footer

schwaluckschwaluck Posts: 103Questions: 27Answers: 1

Hi all,
I am using the following code to calculate the total of a column:

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

    function numberWithCommas(x) {
        return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, '.');
    };

            total = api
               .column(3, { search: 'applied'} ) 
               .data()
               .reduce( function (a, b) {
                   return numberWithCommas(intVal(a) + intVal(b));
               }, 0 );

$( api.column( 3 ).footer() ).html(
                'Summe: ' + total
            );

It works perfectly fine besides the fact, that numbers are displayed like this:
1.000.20

Whereelse it should be:
1.000,20

So the thousand seperator should be a point (like it is now), where the decimal seperator needs to be a comma.

Can anyone help me to achieve this?

Thanks for your time!!!

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,949Questions: 87Answers: 416
    Answer ✓

    data tables has a method to render numbers like this:

    var numberRenderer;
    if (lang == 'de') {
        numberRenderer = $.fn.dataTable.render.number( '.', ',', 2 ).display;
    } else {
        numberRenderer = $.fn.dataTable.render.number( ',', '.', 2 ).display;
    }
    
    var formattedNumber = numberRenderer(yourUnformattedNumber);
    
This discussion has been closed.