Sum result is NaN

Sum result is NaN

sakuraclampsakuraclamp Posts: 7Questions: 1Answers: 0

When I sum TOTAL column, result is Nan
I'm using this section on my code and ı wanna change as float.
@string.Format("{0:N}", item.TOTAL_NET)

How can I fix?
thnx.

I'm using this section for sum.

This question has accepted answers - jump to:

Answers

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    You are using non-numeric numbers. Hence it cannot work. The world of software is US dominated. So 1.019,52 is just some string, but not a number. 1,019.52 would be a proper number. And 1019.52 too.

    In your intVal function (which I would call floatVal by the way) you are replacing periods with spaces. So you get 1019,52 which is not a number. You need to replace
    1. the periods with spaces and then
    2. the remaining comma with a period

    return typeof i === 'string' ? i.replace(/[\.]/g, '').replace(/[\,]/g, '.') * 1 : typeof i === 'number' ? i : 0;
    

    P.S.: I didn't check the rest of your code. Just give it a try first.

  • sakuraclampsakuraclamp Posts: 7Questions: 1Answers: 0
    edited November 2022

    This code call data as string. But before I tried parse to float, I need to replace.
    Like that.

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

    But this is not working. What should I do?

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406
    edited November 2022

    Not sure I understand what you are talking about ... This code should give you the footer with the total in the same formatting as your input numbers.

    footerCallback: function (row, data, start, end, display) {                
        var api = this.api(); 
        var numberRenderer = $.fn.dataTable.render.number( '.', ',', 2 ).display;
    
        // Remove the formatting to get float data for summation
        var floatVal = function (i) {
            return typeof i === 'string' ? i.replace(/[\.]/g, '').replace(/[\,]/g, '.') * 1 : typeof i === 'number' ? i : 0;
        };
    
        // Total over current page
        var total = api
            .column( 9, { page: 'current' } )
            .data()
            .reduce(function (a, b) {
                return floatVal(a) + floatVal(b);
            }, 0);
        // Update footer
        $( api.column(9).footer() ).html( numberRenderer(total) );
    }
    

    This would be the result. The green line is the footer.

    The full code for the example above is this. It is flexible regarding the user language (English and German; German being the same format as yours)

    footerCallback: function (row, data, start, end, display) {                
        var api = this.api(); 
        if (lang == 'de') {
            var numberRenderer = $.fn.dataTable.render.number( '.', ',', 2 ).display;
        } else {
            var numberRenderer = $.fn.dataTable.render.number( ',', '.', 2 ).display;
        }
    
        // Remove the formatting to get integer data for summation
        var floatVal = function (i) {
            if (lang == 'de') {
                return typeof i === 'string' ? i.replace(/[\.]/g, '').replace(/[\,]/g, '.') * 1 : typeof i === 'number' ? i : 0;
            } else {
                return typeof i === 'string' ? i.replace(/[\,]/g, '') * 1 : typeof i === 'number' ? i : 0;
            }
        };
    
        // Total over all pages
        var twoDecPlacesCols = [3, 6];
        var total;
        var curr = data[0].ctr.currency;
        for ( i=0; i < twoDecPlacesCols.length; i++ ) {
            total = api
                .column(twoDecPlacesCols[i])
                .data()
                .reduce(function (a, b) {
                    return floatVal(a) + floatVal(b);
                }, 0);
            // Update footer
            $(api.column(twoDecPlacesCols[i]).footer()).html( numberRenderer(total) + " " + curr );
        }
    }
    
  • sakuraclampsakuraclamp Posts: 7Questions: 1Answers: 0

    When I use your code, nothing is returning on sum

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    ... Then you have a mistake somewhere ...

    Please post a test case as per the forum rules. Thanks.

  • sakuraclampsakuraclamp Posts: 7Questions: 1Answers: 0

    WEll, I call to Total_NET as a string, it's not numeric. But before I sum them, I need to convert as numeric.
    I can use $.fn.dataTable.render.number('.', ',', 2, '') But when I use that,it doesn't seen after the ' , '

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406
    Answer ✓

    WEll, I call to Total_NET as a string

    What does that mean? I don't understand it..

    If you call the number renderer with something in the format 9999.99 it should work. And yes the number renderer does not return a number because a formatted number 9.999,99 is not a number. Hence it's gotta be a string.

    In your debugger please check the format of "total". If it is not 9999.99 you have a mistake somewhere.

  • sakuraclampsakuraclamp Posts: 7Questions: 1Answers: 0

    if i change the question, I called that column as numeric and sum is correct. But I see '##,00'.
    How can I fix 00

  • sakuraclampsakuraclamp Posts: 7Questions: 1Answers: 0

    It's code

        $(document).ready(function () {
            $('#example').DataTable({
                dom: 'lBfrtip',
                scrollX: true,
    
                footerCallback: function (row, data, start, end, display) {
                    var api = this.api();
                    var numberRenderer = $.fn.dataTable.render.number('.', ',', 2).display;
    
                    // Remove the formatting to get float data for summation
                    var floatVal = function (i) {
                        return typeof i === 'string' ? i.replace(/[\.]/g, '').replace(/[\,]/g, '.') * 1 : typeof i === 'number' ? i : 0;
                    };
    
                    // Total over current page
                    var total = api
                        .column(9, { page: 'current' })
                        .data()
                        .reduce(function (a, b) {
                            return floatVal(a) + floatVal(b);
                        }, 0);
                    // Update footer
                    $(api.column(9).footer()).html(numberRenderer(total));
                }
            });
        });
    
  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    But I see '##,00'.
    How can I fix 00

    What does that mean?
    To me your pic looks good.

    Don't seem to understand you very well. Sorry about that.

  • sakuraclampsakuraclamp Posts: 7Questions: 1Answers: 0

    for example first row should be 1.019,52 but it seen 1.019,00

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    The code I gave you and what you posted does not manipulate the column values, only the footer. Hence I have no idea why your decimals are suddenly gone in your data table.

  • allanallan Posts: 61,452Questions: 1Answers: 10,055 Site admin
    Answer ✓

    If you give me a link to a page showing the issue I can take a look into it.

    Allan

Sign In or Register to comment.