Web Development
Building E-Commerce Discount Systems: Edge Cases and Math
Stacking discounts, pre-tax vs post-tax application, rounding at scale, and preventing common discount logic bugs in online stores.
Discount Stacking Order Matters
Applying 20% off then $10 off gives a different result than $10 off then 20% off. Example: $100 item. First scenario: 20% off = $80, then $10 off = $70 ($30 total savings). Second: $10 off = $90, then 20% off = $72 ($28 total savings). The order of application must be consistent and documented. Most e-commerce platforms apply percentage discounts before fixed-amount discounts.
Tax Calculation With Discounts
In most jurisdictions, sales tax applies to the discounted subtotal, not the original price. Apply the discount first, then compute tax on the result. If tax is applied before discount, the merchant over-collects tax, which can cause compliance issues. Our discount calculator shows the correct order: original → discount(s) → discounted subtotal → tax → final total.
Rounding at Scale
A 10% discount on a $9.99 item = $0.999, rounding to $1.00. At 10,000 orders per day, the rounding difference is $10/day — significant at scale. Use banker's rounding (round-half-to-even) for financial calculations as specified by IEEE 754, or truncate toward zero for tax compliance. Never use round-half-up for transaction amounts in regulated industries.