-2

I've succesfully used this code to calculate sums in a table:

var $overall = 0;

$("tr.sum").each(function()
{

    var $qnt = $(this).find("td").eq(0);
    var $price = $(this).find("td").eq(1);

    console.log($qnt+" | "+$price);

    var sum = parseFloat($price.text()) * parseFloat($qnt.text());

    $(this).find("td").eq(2).text(sum);

    $overall+= sum;

});

$("#total").text($overall); });

I changed one line to avoid rounding errors:

var sum = parseFloat((parseFloat($price.text().replace(',', '.')) * parseFloat($qnt.text().replace(',', '.'))).toFixed(2));

It works fine. But I can't solve the problem to round the total sum to two decimal places.

 $("#total").text($overall);

I tried toFixed(2). But this results in a string and not a number.

Any help would be appreciated!

Thanks, Mike

user2516117
  • 91
  • 1
  • 4
  • 12

1 Answers1

-2

after doing all calculation make it like this

$overall.toFixed(2);
  • What is wrong, if sum/overall = 10.12432, then after passing it toFixed(sum); it will return 10.12 – Manoj Kumar Dharani Jun 24 '13 at 12:41
  • Thank you very much, guys. $overall.toFixed(2); did the trick. Unfortunately, my question was marked as duplicate. Well, that's not exactly true. I knew .toFixed(2), but I didn't know how to exacly use it in this example. Well, it doesn't work in IE. But then, what works in IE? Again, thanks a lot! – user2516117 Jun 24 '13 at 13:55