Margin vs markup in JavaScript: formulas, edge cases, and pricing for a target margin after fees
If you build anything that touches pricing (a store admin, an invoice tool, a seller calculator) you will eventually get a bug report that says "I set a 50% margin and I'm only making a third." The code is usually fine. The problem is that the user said margin and the code computed markup, or the other way round. I ran into this while building the profit margin calculator on DailyToolLab, a set of free tools that run in the browser. This post walks through the math, the edge cases that break naive implementations, and how to solve for a selling price when marketplace fees are a percentage of that same price. Two percentages, one profit Let C be cost and P be selling price. Profit is P - C in both cases. Only the denominator changes: Margin % = (P − C) / P × 100 (share of the selling price you keep) Markup % = (P − C) / C × 100 (how much you added on top of cost) Cost Price Profit Margin Markup 50 100 50 50% 100% 100 150 50 33.33% 50% 100 200 100 50% 100%
If you build anything that touches pricing (a store admin, an invoice tool, a seller calculator) you will eventually get a bug report that says "I set a 50% margin and I'm only making a third." The code is usually fine. The problem is that the user said margin and the code computed markup, or the other way round.
I ran into this while building the profit margin calculator on DailyToolLab, a set of free tools that run in the browser. This post walks through the math, the edge cases that break naive implementations, and how to solve for a selling price when marketplace fees are a percentage of that same price.
Two percentages, one profit
Let C be cost and P be selling price. Profit is P - C in both cases. Only the denominator changes:
- Margin % = (P − C) / P × 100 (share of the selling price you keep)
- Markup % = (P − C) / C × 100 (how much you added on top of cost)
| Cost | Price | Profit | Margin | Markup |
|---|---|---|---|---|
| 50 | 100 | 50 | 50% | 100% |
| 100 | 150 | 50 | 33.33% | 50% |
| 100 | 200 | 100 | 50% | 100% |
The classic mistake is row two: someone wants a 50% margin, adds 50% to cost, and ends up with 33.33%. To get a real 50% margin on a cost of 100 you need a price of 200, which is a 100% markup.
The basic functions
function margin(cost, price) {
if (!(price > 0)) return NaN; // margin undefined at price 0
return ((price - cost) / price) * 100;
}
function markup(cost, price) {
if (!(cost > 0)) return NaN; // markup undefined at cost 0
return ((price - cost) / cost) * 100;
}
Returning NaN (and rendering it as "—" in the UI) is better than returning Infinity or 0. A free sample with zero cost has an undefined markup, not an infinite one. With a positive price and zero cost, margin is simply 100%.
Converting between them
You often get one number and need the other. With both written as percentage numbers (50, not 0.5):
const marginFromMarkup = (u) => (100 * u) / (100 + u);
const markupFromMargin = (m) => {
if (m >= 100) return NaN; // no finite markup reaches 100% margin
return (100 * m) / (100 - m);
};
marginFromMarkup(50); // 33.33
marginFromMarkup(100); // 50
markupFromMargin(50); // 100
markupFromMargin(60); // 150
A small lookup table is handy for tests:
| Markup % | Margin % |
|---|---|
| 25 | 20 |
| 50 | 33.33 |
| 100 | 50 |
| 150 | 60 |
Price from a target margin
Given cost and a target margin m as a decimal (0.4 for 40%):
P = C / (1 − m) valid only when 0 ≤ m < 1
function priceForMargin(cost, marginPct) {
const m = marginPct / 100;
if (m < 0 || m >= 1) return NaN; // reject impossible targets
return cost / (1 - m);
}
priceForMargin(100, 50); // 200
priceForMargin(20, 60); // 50
Validate this input. A target margin of 100% or more has no finite positive price when cost is positive, and cost / 0 will happily hand you Infinity if you let it.
Adding fees: the part most calculators skip
Real sellers do not keep P - C. They pay a percentage fee on the sale price (marketplace commission plus payment processing), often a fixed fee per order, sometimes shipping, and in some countries the price already includes VAT or GST.
The tricky part is that the percentage fee depends on the price you are trying to find. You cannot compute fees first and then add the margin on top. You have to solve for P.
Define:
-
C= product cost -
S= shipping and packaging you pay -
F= fixed fee per order -
f= percentage fees as a decimal (e.g. 0.095) -
t= VAT/GST rate included in the price, as a decimal -
m= target net margin as a decimal
If tax is included in the price, the tax portion is P × t / (1 + t). Net profit is:
profit = P − C − S − F − f·P − P·t/(1+t)
We want profit = m · P. Collect the P terms:
P · (1 − f − t/(1+t) − m) = C + S + F
P = (C + S + F) / (1 − f − t/(1+t) − m)
function priceForNetMargin({ cost, shipping = 0, fixedFee = 0,
feePct = 0, taxPct = 0, marginPct }) {
const f = feePct / 100;
const t = taxPct / 100;
const m = marginPct / 100;
const taxShare = t / (1 + t);
const denom = 1 - f - taxShare - m;
if (denom <= 0) return NaN; // fees + tax + margin eat the whole price
return (cost + shipping + fixedFee) / denom;
}
// cost 20, $5 shipping, $0.30 fixed fee, 9.5% fees, target 40% net margin
priceForNetMargin({ cost: 20, shipping: 5, fixedFee: 0.3,
feePct: 9.5, marginPct: 40 }); // ≈ 50.10
Check it: at P ≈ 50.10, fees are ≈ 4.76, total costs ≈ 30.06, profit ≈ 20.04, and 20.04 / 50.10 ≈ 40%. The same denominator check also gives you a clear error message: if percentage fees plus included tax plus the target margin reach 100% of the price, no price works.
Setting m = 0 in the same formula gives the break-even price, which is worth showing next to every result so users can see how close they are to a loss.
Gross vs net: label them
Once fees are in play you have two honest margins:
- Gross margin: (P − C) / P. Product cost only.
- Net margin: after shipping, fees and included tax.
Show both, and label them clearly. Also be careful with the word "net". After-fee proceeds are still not full business profit if rent, ads, returns or the owner's own pay are left out. In the calculator I only claim "net margin after the extra costs you entered", nothing more.
Floating point and rounding
A few practical notes from shipping this:
- Calculate with full precision and round only for display (
toFixed(2)at render time). Rounding intermediate values drifts the margin by a few hundredths. - Do not round a suggested price down. If the exact answer is 50.104, showing 50.10 gives a margin a hair under target. Rounding up to the cent (
Math.ceil(p * 100) / 100) guarantees the target is met. - Parse inputs with
Number()and treat empty fields as 0 for optional costs, but as "missing" for required ones.
Tests that catch the real bugs
console.assert(Math.abs(marginFromMarkup(50) - 33.333) < 0.01);
console.assert(markupFromMargin(50) === 100);
console.assert(priceForMargin(100, 50) === 200);
console.assert(Number.isNaN(priceForMargin(100, 100)));
console.assert(Number.isNaN(markup(0, 10)));
console.assert(margin(0, 10) === 100);
Wrap-up
Margin divides by price, markup divides by cost, and percentage fees force you to solve for the price instead of stacking numbers. Get the denominators right, reject impossible targets, and show gross and net side by side.
If you want a longer plain-English explanation with a conversion chart, I wrote one here: Margin vs markup: difference, formulas and conversion chart. You can also plug your own numbers into the free profit margin calculator. It runs in your browser, with no sign-up.