Everyone has pointed out the issue, caused by .toFixed()
returning a string, but all of those repeated calls to parseFloat()
, toFixed()
and casting are a code smell.
It would be better to keep all of your variables as numbers and just pass them through a function to format to a human readable currency.
// convert to currency
function currencyFormat(amount){
return '£' + (amount*1).toFixed(2);
}
// cast to a number in one place
eventTotal = eventTotal*1;
deliveryTotal = deliveryTotal*1;
// now work with the variables however you need, knowing that you always have numbers
// output currency
totalbox.value = currencyFormat(eventTotal + deliveryTotal);
eventbox.value = currencyFormat(eventTotal);
Now if you need to change the currency symbol or you want to change the decimal separator, or number of decimal places, it's one change in your reusable code. Also, you should use the HTML entity £
instead of using the raw symbol.