I have the variable y, which is a subtotal. Its value is different depending on what happens with the html, but throughout the script I declared it like this:
var y = 21.78;
var tax = (0.055*y).toFixed(2);
var totalprice = y+tax;
/* totalprice holds "21.781.20" instead of 22.98 */
A string representation of number that does not use exponential notation and has exactly digits digits after the decimal place.
y+tax is cast to a string since one of the operands is a string.
I would advise you do all your addition before calling
toFixed(), since the method is most suitable for formatting display output.
var taxRate = 0.055; var subtotal = 21.78; var tax = (taxRate * subtotal).toFixed(2), totalprice = ((1+taxRate) * subtotal).toFixed(2); document.write(totalprice);