Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 2 min read

Minimum deposit in integer cents: round the percentage up, never down

A small calculation with a sharp edge: how much deposit should a business ask for on a big order so its cash never drops below a safety reserve? The rule is short: spare cash = cash in the bank βˆ’ reserve minimum depos

A small calculation with a sharp edge: how much deposit should a business ask for on a big order so its cash never drops below a safety reserve?

The rule is short:

  • spare cash = cash in the bank βˆ’ reserve
  • minimum deposit = up-front costs βˆ’ spare cash (or zero if the spare cash already covers them)

Owners think in whole percentages, so the deposit is shown as a percent of the order. That is where the edge is: the percentage must be rounded up. Rounding to the nearest percent can quietly put the business a few dollars under its own line.

In integer cents

// Minimum deposit in integer cents (BigInt), round deposit % up to a whole percent
const c = d => BigInt(Math.round(d * 100));
const fmt = v => { const s = v < 0n ? '-' : ''; const a = v < 0n ? -v : v; return s + '$' + (a / 100n).toLocaleString('en-US') + '.' + String(a % 100n).padStart(2, '0'); };
const order = c(30000), upfront = c(21000), cash = c(25000), reserve = c(10000);
const spare = cash - reserve;                       // cash you can spend without crossing the reserve
const need = upfront > spare ? upfront - spare : 0n;  // the part of the costs your cash cannot carry
const pct = (need * 100n + order - 1n) / order;       // whole percent, rounded UP
const deposit = order * pct / 100n;
console.log(fmt(spare), fmt(need), pct + '%', fmt(deposit));
console.log('lowest cash, no deposit:', fmt(cash - upfront));
console.log('lowest cash, at minimum:', fmt(cash - upfront + deposit));
// cost of waiting on the gap for 60 days at 12% a year, rounded half up to the cent
const carry = (need * 12n * 60n * 2n + 100n * 365n) / (100n * 365n * 2n);
console.log('cost of waiting:', fmt(carry));

Output:

$15,000.00 $6,000.00 20% $6,000.00
lowest cash, no deposit: $4,000.00
lowest cash, at minimum: $10,000.00
cost of waiting: $118.36

(need * 100n + order - 1n) / order is the integer ceiling of need / order in percent. With BigInt there is no float rounding to argue about, and the lowest-cash figure at the minimum deposit lands exactly on the reserve, which is a good self-check.

Two edge cases worth handling

  • Costs above order value. If the order costs more to deliver than it pays, no deposit makes it safe. Show that first instead of a percentage.
  • Cash already below the reserve. Then even a deposit that covers every cost only stops the order making things worse; say so in words.

The free Deposit Calculator uses this arithmetic in the browser and shows every percentage next to the lowest cash it leaves.

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.