Monday 20 February 2017

javascript - jQuery adding numbers not correctly sum with decimals

I have a js file which calculate all totals when a user click, this will sum up all clicked rows but if the number contains decimal, would not add correctly, instead always put 00 on decimals



function calculateRevenue(){

var sum = 0;
$("input#check_count_revenue[type=checkbox]:checked").each(function() {
sum += parseInt($(this).attr("data-revenuecount"));
});

$("#revenue_count_totals").html(sum.toFixed(2)).digits();
};


Then calling the function like this




table.on("change","input#check_count_revenue", function() {
$(this).closest("td").addClass("checked_revenue");
calculateRevenue();
if ($(this).prop("checked") === false)
$(this).closest("td").removeClass("checked_revenue");
});


If the row contains the following




 12.00
13.00


That would correctly sum 25.00



But



 12.00

13.01


Still got 25.00 instead of 25.01



Any ideas? I already tried parseFloat in place of parseInt, does not resolve

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...