trobbins26 trobbins26 - 1 year ago 46
jQuery Question

hours += parseInt($(this).html()); no outputting decimals

I am currently using this code to calculate the total from a column in a table:

function calculateTotals() {
//these will hold the totals
var hours = 0;

//reference the rows you want to add
//this will not include the header row
var rows = $("#deliverables tr:not(:first-child, :last-child)");
rows.children("td:nth-child(2)").each(function() {
hours += parseInt($(this).html());
});

midHours = (hours).toFixed(2);

$(".total-hours").html(midHours);

};


However, the output
midHours
will not show decimals. For example a table cell with value 12.5 - outputs as 12 in the
.total-hours
cell.

Answer Source

because you are using parseInt(), use parseFloat() instead

var rows = $("#deliverables tr:not(:first-child, :last-child)");
rows.children("td:nth-child(2)").each(function() {
    hours += parseFloat($(this).html());
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download