Answer the question
In order to leave comments, you need to log in
Numbers in Javascript (parseInt, parseFloat)?
Good afternoon!
In the dataTables table, all data is displayed as integers (int).
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);
Answer the question
In order to leave comments, you need to log in
Read the documentation, and do not duplicate questions (yesterday was the same)
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');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question