Selling Online: Prices, Tax, Shipping and Discounts That Add Up
Set up the arithmetic of a small online shop correctly: prices with tax included, rounding per line or per order, shipping tiers and discount codes, calculated with exact decimals, tested on thousands of random baskets and verified with exact fractions.
In this lesson you will
- Compute tax from tax-inclusive prices
- Understand per-line versus per-order rounding
- Apply shipping rules and discounts in a sensible order
- Test shop arithmetic on random baskets
About 28 minutes
Eleven cents that did not match
Priya’s first customer ordered three candles and the shop showed a tax amount that was one cent different from the figure in her accounting program. In an online shop, money arithmetic is full of small decisions, and every shop builder makes them in some way, so you should know what they are and how to test them. The first is whether prices are shown including tax or excluding it. In many countries, consumer prices must include value added tax, VAT, so that a candle at 24.99 includes tax, and the shop must work out how much of it is tax: with a 20 percent rate, the tax inside a gross price is the price times 20 / 120. (The rate here is an example, and rates differ by country and product, so check the rules that apply to you.)
The second decision is rounding. Each result must be a whole number of cents, so when is it rounded: for every line of the basket, or once for the whole order? The two methods can differ by a few cents, and accounting rules usually specify which to use. Third, the order of operations: a discount code, shipping and tax must be applied in a defined order, and a “10 percent off” code applied before or after shipping gives different totals. Fourth, shipping rules such as “free over 50 euros” depend on which amount is compared, before or after the discount. A good shop builder lets you choose, and you should test the choices with real numbers, in cents, before the first customer does.
| Decision | Options | Typical rule |
|---|---|---|
| Tax in prices | Included or excluded | Included for consumers in many countries |
| Rounding | Per line or per order | Follow your accountant or tax authority |
| Discount order | Before or after tax and shipping | Documented by the builder |
| Free shipping threshold | Before or after discounts | Check the setting |
| Money type | Integer cents or decimals | Never floats |
from decimal import Decimal, ROUND_HALF_UP
import random, json
VAT = Decimal("0.20")
def cents(x): return int(x.quantize(Decimal(1), rounding=ROUND_HALF_UP))
def vat_in(gross_cents): return cents(Decimal(gross_cents) * VAT / (1 + VAT))
def basket(lines, code=None, ship_free_from=5000, ship=495, method="line"):
# lines: list of (unit_gross_cents, qty). Prices include VAT.
gross = sum(p * q for p, q in lines)
discount = cents(Decimal(gross) * Decimal("0.10")) if code == "WELCOME10" else 0
goods = gross - discount
shipping = 0 if goods >= ship_free_from else ship
total = goods + shipping
if method == "line": # tax per line (after discount share), then shipping
share = [cents(Decimal(p * q) * Decimal(goods) / Decimal(gross)) for p, q in lines] if gross else []
share[-1] += goods - sum(share) if share else 0 # push the rounding remainder into the last line
tax = sum(vat_in(s) for s in share) + vat_in(shipping)
else: # tax once on the whole order
tax = vat_in(total)
return {"gross": gross, "discount": discount, "shipping": shipping, "total": total, "vat": tax, "net": total - tax}
b = basket([(2499, 3), (1250, 1)], code="WELCOME10")
print("3 candles at 24.99 and a wick trimmer at 12.50 with the code WELCOME10:", b)
print("same basket without the code:", basket([(2499, 3), (1250, 1)]))
print("free shipping threshold uses the discounted goods: a 52.00 basket with the code ->", basket([(5200, 1)], code="WELCOME10")["shipping"], "cents shipping")
rng = random.Random(1)
diffs = {}; bad = 0
for _ in range(10000):
lines = [(rng.randrange(199, 7999), rng.randrange(1, 6)) for _ in range(rng.randrange(1, 6))]
code = rng.choice([None, "WELCOME10"])
a, c = basket(lines, code, method="line"), basket(lines, code, method="order")
assert a["total"] == c["total"]
diffs[abs(a["vat"] - c["vat"])] = diffs.get(abs(a["vat"] - c["vat"]), 0) + 1
if a["net"] + a["vat"] != a["total"]: bad += 1
print("10000 random baskets: totals always equal; difference in the tax amount between the two methods (cents -> baskets):", dict(sorted(diffs.items())), "| baskets where net + tax != total:", bad)
print("DATA " + json.dumps({"b": b, "diffs": {str(k): v for k, v in sorted(diffs.items())}, "bad": bad}))
3 candles at 24.99 and a wick trimmer at 12.50 with the code WELCOME10: {'gross': 8747, 'discount': 875, 'shipping': 0, 'total': 7872, 'vat': 1313, 'net': 6559}
same basket without the code: {'gross': 8747, 'discount': 0, 'shipping': 0, 'total': 8747, 'vat': 1458, 'net': 7289}
free shipping threshold uses the discounted goods: a 52.00 basket with the code -> 495 cents shipping
10000 random baskets: totals always equal; difference in the tax amount between the two methods (cents -> baskets): {0: 6632, 1: 3274, 2: 94} | baskets where net + tax != total: 0
The basket of three candles at 24.99 and a wick trimmer at 12.50 comes to 87.47 before the code. The code WELCOME10 takes 10 percent off the goods, 8.75 (8.747 rounded half up), leaving 78.72. Because the discounted goods are above the free-shipping threshold of 50.00, shipping is 0.00. The tax contained in the total, at 20 percent included in the price, is 13.13, and the net amount is 65.59. The program does everything in whole cents with the decimal type, and rounds half up in one place, so every figure can be reproduced by hand.
The last two experiments show why the details matter. A 52.00 basket with the code WELCOME10 becomes 46.80, which is under the threshold, so shipping of 4.95 is charged: the threshold was compared with the discounted amount, a choice that can annoy a customer who sees “free shipping over 50” and a code at the same time. A builder may compare before the discount, and you should test which it does. Then 10,000 random baskets were priced with tax calculated per line and with tax calculated once on the order. The totals were identical in every basket, because rounding changes only how the tax is allocated and not the money. The tax amounts differed between the methods in some baskets: the number of baskets by size of difference in cents was {‘0’: 6632, ‘1’: 3274, ‘2’: 94}, so the two methods agree or differ by a cent or two, which is exactly the kind of difference that annoyed Priya. The net amount and the tax added up to the total in all baskets, 0 exceptions.
Independent check: the basket was recomputed with exact fractions, with no decimal rounding library and rounding half up in one explicit step, and the gross amount of 8,747 cents, the discount of 875 and the total of 7,872 agree. The invariants, totals equal between methods and net plus tax equal to total, were checked by the program over all 10,000 random baskets. A shop owner can do the same with a spreadsheet, and the test of ten or twenty real-looking baskets against your accountant’s figures is the best possible check before launch.
Check by hand: 3 x 24.99 = 74.97, plus 12.50 is 87.47. Ten percent of 87.47 is 8.747, which rounds to 8.75, and 87.47 minus 8.75 is 78.72. The tax inside 78.72 at 20 percent included, calculated once on the whole order, is 78.72 x 20 / 120 = 13.12. The program above calculated the tax line by line and got 13.13, one cent more, because each line’s tax is rounded separately, and then net amount of 65.59 plus tax of 13.13 is the total of 78.72. This one-cent difference between two correct methods is exactly the kind that surprised Priya.
Use integer cents or decimals. The program above never touches a float, and the builder you choose should not either: test it with prices like 24.99 and rates like 20 percent.
Before launch, order ten typical baskets, including a discount code and a free-shipping case, and compare the order totals and the tax with figures calculated by your accountant.
Builders, plans and prices change often, so the lessons use generic labels such as Builder A and describe categories of tools, and you must check each vendor’s current website for features, prices and limits. Every calculation and check in this lesson was run while the course was written, in Python 3.13 or Node.js 22, on sample data that the lesson provides, with a second independent calculation. Tax rules, rates, rounding requirements and invoicing obligations differ by country and product, and change. This lesson is not tax advice: check the rules that apply to you with an accountant or the tax authority.
Practice
Add a second tax rate for a product category and test a mixed basket. Change the free-shipping threshold to compare before the discount, and see which baskets change. Write the ten baskets you would test before launch.
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
What is the tax inside a gross price at 20 percent?
- Gross x 20 / 100
- Gross x 0.8
- Gross – 20
- Gross x 20 / 120
The rate over one plus the rate.
Why can per-line and per-order tax differ?
- The rates differ
- Customers differ
- Rounding is applied at different points
- Shipping differs
By a cent or two.
How should money be stored?
- As floating point numbers
- As text only
- In integer cents or exact decimals
- As images
Avoid binary floats.
Why does the order of discount, shipping and tax matter?
- It does not matter
- The totals and thresholds differ
- Browsers decide it
- It changes the language
Test your builder’s settings.
What is a good pre-launch test?
- Order one item
- Order ten typical baskets and compare with your accountant
- Skip testing
- Ask a friend to guess
Include a code and free shipping.
Summary
- Prices with tax included contain tax = gross x rate / (1 + rate).
- Rounding per line or per order can differ by a cent or two.
- Define the order of discount, shipping and tax, and test the thresholds.
- Test baskets with an accountant before launch.
Questions
Which tax settings do I need?
The rates and rules of the countries where you sell. Some regions have special rules for online sales to consumers. Ask an accountant or your tax authority.
Should I show prices with or without tax?
Rules differ: consumer prices must include tax in many countries, and business customers often expect prices without it. Follow the rules for your market.
How do I handle currencies?
Show prices in the currency your customers expect, and know how your payment provider converts, including fees. Check the minor units of each currency, as the API integration course explained.
