Home › Forums › JavaScript › Multi product example decimals › Reply To: Multi product example decimals
November 4, 2016 at 9:58 pm
#247514
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);