Tuesday, June 26, 2018

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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...