Home > OS >  JavaScript round val to 2 decimal places
JavaScript round val to 2 decimal places

Time:11-06

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...

CodePudding user response:

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