Pricing and total calculations sit at the heart of every line-item-driven document. Get them right and your invoices are trustworthy and accurate; get them wrong and you have a financial mess. This article covers the most common patterns.
Order of Operations
A typical calculation order:
- For each line: compute line total = qty × unit_price × (1 - line_discount).
- Subtotal = sum of line totals.
- Apply cart-level discount (fixed or percentage) to the subtotal.
- Compute tax — either per-line (then summed) or on the discounted subtotal.
- Grand total = discounted subtotal + tax.
Tax Modes
- Tax-exclusive: Tax added on top. Most common in B2B.
- Tax-inclusive: Tax baked into unit price; subtotal and total are the same, with tax shown as “of which X is VAT”.
- Mixed: Some lines inclusive (consumer items), others exclusive (services) — unusual but supported.
Rounding
- Round each line total to 2 decimals (or 4 for high-precision currencies).
- Sum the rounded line totals — gives stable subtotal.
- Round the final total once at the end (avoids cumulative rounding error).
- Match the rounding mode (half-up vs half-even) to your accounting standards.
Worked Examples
- UK B2B invoice: Two lines £500 + £750, 10% cart discount, 20% VAT exclusive. Subtotal £1250; after discount £1125; VAT £225; total £1350.
- EU consumer (tax-inclusive): Two lines €120 + €60. Subtotal €180; “of which €30 is VAT (@ 20%)”.
- Multi-currency quote: Three lines in USD, two in EUR. Each line total in its own currency. Final total normalised to client's home currency using today's FX rate, with rate captured in the saved record.
- Discount-then-tax vs tax-then-discount: Same gross figures, different tax outcomes — choose the order that matches your jurisdiction's rules.
Warning: Always store the underlying line items, not just the totals. If a tax rate later changes you can recalculate; if you only kept the totals you lose precision and audit trail.