Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Multi product example decimals Reply To: Multi product example decimals

#247514
Mottie
Member

In javascript, you can use the toFixed function to do the formatting you desire.

  • Please make sure you pay attention to how it rounds the value.
  • This function converts a number type value into a string, so before performing any calculations, make sure to parse the string back into a number value.
var tax = .075,
  cost = 10.345,
  total = cost.toFixed(2);

// total = 10.35
$('#sub-total-container').text('$ ' + total);

// use rounded total to calculate tax
cost = parseFloat(total);
total = cost + ( cost * tax);
total = total.toFixed(2);

// total = 11.13
$('#total-container').text('$ ' + total);