I have some javascript code that caluclates sales tax on a web form and totals everything up. I am setting the number to decimals using toFixed(2) and adding the calculated tax to my subtotal using basic addition. The end result is off by 1 cent. My subtotal and tax amount field show the correct amount but my total field does not.
As a workaround, I changed my subtotal calculation to .toFixed(3), but am curios how I can correctly get this to calculate and display properly with 2 decimals. I am not that good with javascript so am stumped on this.
Here is my javascript (Not showing everything, just the relevant code):
var taxper = document.autoSumForm.taxRate.value*1.0;
tax = subtotal*(taxper/100).toFixed(3);
document.autoSumForm.subtotal.value = subtotal.toFixed(2);
document.autoSumForm.tax.value = tax.toFixed(3);
And here is an example calculation:
Taxper = 6.625%
Subtotal = $20.00
Tax = 6.625% * $20.00
Tax amount = 1.32
Total = $21.32 <---- This displays $21.33 if I don't set subtotal calculation to toFixed(3)