A customer offers a big order. You pay materials and labour now, they pay later. How much deposit do you need so the job never pushes your bank balance below the reserve you keep for payroll and rent?

It is one subtraction, but there are two details that are easy to get wrong in code: floating-point money and the direction of rounding.

The formula

spare cash = cash − reserve minimum deposit = max(0, up-front costs − spare cash) minimum deposit % = minimum deposit ÷ order value, rounded UP to a whole percent 

In integer cents

// all amounts in integer cents (BigInt), never floats function minDeposit({ orderValue, upfrontCosts, cash, reserve }) { const spare = cash - reserve; // can be negative let need = upfrontCosts - spare; if (need < 0n) need = 0n; // percent rounded UP: ceil(need * 100 / orderValue) const pct = need === 0n ? 0n : (need * 100n + orderValue - 1n) / orderValue; const coverAllPct = (upfrontCosts * 100n + orderValue - 1n) / orderValue; return { need, pct, coverAllPct }; } const c = (dollars) => BigInt(Math.round(dollars * 100)); console.log(minDeposit({ orderValue: c(30000), upfrontCosts: c(18000), cash: c(20000), reserve: c(8000), })); // { need: 600000n, pct: 20n, coverAllPct: 60n } -> $6,000, 20%, 60% 

Why round the percent up

If the exact answer is 20.3%, quoting 20% leaves you $90 short on a $30,000 order: still below the reserve. A deposit percentage is a floor you will say out loud to a customer, so it must be the first whole percent that is enough, not the nearest one. (a + b − 1) / b is ceiling division for positive integers, with no floats involved.

Why max(0, …)

When spare cash already covers the costs, the subtraction is negative. A negative deposit is meaningless; the honest answer is "no deposit needed for cash reasons" (you may still want one for commitment).

Two numbers, one negotiation

pct is the floor that protects your reserve. coverAllPct is the deposit at which the customer, not your bank balance, funds the job. Anything between them is a trade: each percent below coverAllPct is cash you lend to the customer until delivery.

A free browser version with a step table is here: Deposit Calculator (no sign-up, nothing stored).