R
R
Roman-OverOffers2014-02-15 14:31:03
Python
Roman-OverOffers, 2014-02-15 14:31:03

Numbers in Javascript (parseInt, parseFloat)?

Good afternoon!
In the dataTables table, all data is displayed as integers (int).
a72a8718ca13b5a0a089c820833b8cb5.png

But the Totals calculation script returns NaN.

JSON returns data like this,

"unique_clicks": "1001", //iClicks
"events_count": 38, //iConv
"pending_revenue": 1000.0, //iPendingRevenue
"approved_revenue": 1500.0 //iApprovedRevenue

Then they are parsed here:

var stat_options = $.extend({
    "sAjaxSource": "/dashboard/graph/", 
    "aoColumns": [
        { "mData": "date" }, 			
        // {"mData":"clicks"}, 
        { "mData":"unique_clicks" }, 
        { "mData":"events_count" },
        { "mData":"approved" },
        { "mData":"pending" },
        { "mData":"rejected" }, 
        { "mData":"cpc" }, 
        { "mData":"ltr" }, 
        { "mData":"approval_rate" }, 
        // {"mData":"total_revenue"}, 
        // {"mData":"rejected_revenue"},
        { "mData":"pending_revenue" },
        { "mData":"approved_revenue" }  
    ], 
    "fnFooterCallback": function( nRow, aaData, iStart, iEnd, aiDisplay ) {
                var iClicks = 0
                var iConv = 0
                var iPendingRevenue = 0
                var iApprovedRevenue = 0
                for ( var i=0 ; i<aaData.length ; i++ )
                {
                    iClicks += aaData[i][1]*1;
                    iConv += aaData[i][3]*1;
                    iPendingRevenue += aaData[i][9]*1;
                    iApprovedRevenue += aaData[i][10]*1;
                }
                var nCells = nRow.getElementsByTagName('th');
                nCells[1].innerHTML = parseInt(parseFloat(iClicks));
                nCells[3].innerHTML = parseInt(parseFloat(iConv));
                nCells[9].innerHTML = parseInt(parseFloat(iPendingRevenue));
                nCells[10].innerHTML = parseInt(parseFloat(iApprovedRevenue));
                }
}, tables_options);


But neither parseFloat nor parseIng help.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nazar Mokrinsky, 2014-02-15
@nazarpc

Read the documentation, and do not duplicate questions (yesterday was the same)

S
Sergey, 2014-02-15
Protko @Fesor

advice for the future, if you are already using parseInt / parseFloat, then always try to put down the second argument (the base of the number system), because in the end it will sometimes lead to extremely disgusting and hard-to-find bugs. It is better to use explicit type casting through Number.

var myFloatVal= Number('4.4'),
      myIntVal =  Number('4');

D
dmitrytm, 2014-04-02
@dmitrytm

Hello Roman-OverOffers. Is there any way to contact you about JSON?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question