"Charge cost plus 25%" and "keep a 25% margin" are different formulas, and pricing code often implements the first while the UI says the second.
Full cost per unit
const direct = 1800n; // $18.00 const fixed = 600000n; // $6,000.00 a month const units = 400n; const ceilDiv = (a, b) => (a + b - 1n) / b; const fixedShare = ceilDiv(fixed, units); // 1500n const breakEven = direct + fixedShare; // 3300n = $33.00 Rounding the fixed share up matters: rounding down means the floor never quite covers fixed costs.
Margin is on the price
Margin m means profit / price = m, so price = cost / (1 - m). With m as basis points:
const marginBps = 2500n; // 25% const floor = ceilDiv(breakEven * 10000n, 10000n - marginBps); // 4400n = $44.00 A markup of 25% gives 3300n * 125n / 100n = 4125n, which is only a 20% margin. If your field is labelled "margin", test that profit * 10000 / price >= marginBps for the result.
Edge cases worth a test
-
marginBps >= 10000n→ no finite price; return an explicit error state -
units = 0→ fixed share undefined; ask for volume instead of dividing - exact division vs one cent of remainder, to prove the ceiling
- the floor at half the volume, because fixed share doubles
The free Minimum Price Calculator uses this approach and also adds the cost of waiting to be paid.