Vogal
Vogal

Reputation: 104

JavaScript round val to 2 decimal places

I have a script to calculate checkboxes as they are selected:

$('input:checkbox').change(function () {
  var total = 0;
  $('input:checkbox:checked').each(function () {
    total += isNaN(parseInt($(this).val())) ? 0 : parseInt($(this).val());
  });
  $("#total").val(total);
});

The total is showing as 46 rather than 46.00 for example. How can I ensure the number is always showing to 2 decimal places?

I added

console.log(format(total));

Which showed the correct format...

Upvotes: 0

Views: 242

Answers (1)

404Exist
404Exist

Reputation: 99

total = parseFloat(total).toFixed(2);

Upvotes: 2

Related Questions