Compound Interest: The Math Behind Wealth Building
Try it live: Want to skip the maths and see your own numbers? Run them through the free Compound Interest Calculator — no signup, no data stored, just the result.
If you put £100 in a savings account at 5% interest, you earn £5 in year one. Simple. But in year two, you earn interest not just on the original £100, but on the £105 now sitting in the account. That small difference — interest earning interest — is compound interest, and over decades it is the single most powerful force in personal finance.
It is also a beautifully small piece of mathematics. The whole thing fits in one formula, and once you can code it, you can answer questions no bank brochure will answer for you: How long until my money doubles? What does an extra £50 a month actually buy me in 30 years? What does a 1% fee really cost?
The core formula
Compound interest is defined by four inputs:
- P — the principal, your starting amount
- r — the annual interest rate, as a decimal (5% = 0.05)
- n — how many times per year interest compounds
- t — the number of years
The future value is:
A = P × (1 + r/n)^(n×t)
Here is that formula in JavaScript, with nothing hidden:
function compoundInterest(principal, annualRate, years, compoundsPerYear = 12) {
return principal * Math.pow(1 + annualRate / compoundsPerYear, compoundsPerYear * years);
}
console.log(compoundInterest(1000, 0.05, 10)); // ≈ 1647.01
£1,000 at 5%, compounded monthly for 10 years, becomes about £1,647. The £647 of growth did not arrive evenly: in year one you earned roughly £51, but in year ten you earned roughly £78, because each year's interest was calculated on a bigger balance. That acceleration is the whole point.
Adding regular contributions
Almost nobody invests a lump sum and then stops. The realistic version adds a monthly contribution, and this is where the numbers get genuinely surprising:
function futureValueWithContributions(principal, monthly, annualRate, years) {
const r = annualRate / 12;
const n = years * 12;
const growthOfPrincipal = principal * Math.pow(1 + r, n);
const growthOfContributions = monthly * ((Math.pow(1 + r, n) - 1) / r);
return growthOfPrincipal + growthOfContributions;
}
// £0 starting balance, £200/month, 7% return, 30 years
console.log(futureValueWithContributions(0, 200, 0.07, 30)); // ≈ 243,994
You paid in £72,000 over 30 years (£200 × 360 months). The final balance is roughly £244,000. The extra £172,000 is compounding doing the heavy lifting — and it is heavily back-loaded. After 10 years you would have only about £34,000. The last decade alone adds more than £130,000. This is why starting early beats starting big: time in the market is the input you cannot buy back later.
The Rule of 72
There is a classic shortcut worth knowing, because it makes doubling times intuitive. Divide 72 by your annual return percentage to estimate the years needed to double your money:
function yearsToDouble(annualRatePercent) {
return 72 / annualRatePercent;
}
console.log(yearsToDouble(5)); // ≈ 14.4 years
console.log(yearsToDouble(7)); // ≈ 10.3 years
console.log(yearsToDouble(10)); // ≈ 7.2 years
The gap between 5% and 10% does not look dramatic as a percentage. As a doubling time, it is the difference between waiting 14 years and waiting 7. Small rate differences compound into life-changing differences — which is also why fees matter so much.
The quiet cost of fees
A 1% annual fee sounds trivial. Run it through the same formula and it is anything but:
const noFee = futureValueWithContributions(0, 200, 0.07, 30);
const withFee = futureValueWithContributions(0, 200, 0.06, 30);
console.log(noFee); // ≈ 243,994
console.log(withFee); // ≈ 200,903
That 1% fee costs about £43,000 — nearly 18% of the final balance. The fee compounds against you in exactly the same way returns compound for you. Whenever you compare investment products, compare them after fees, over the full horizon, not on headline rates.
Edge cases developers get wrong
1. Compounding frequency barely matters at low rates, but the code must still handle it. Daily vs monthly compounding on £1,000 at 5% for one year differs by pennies. Do not let a user-facing calculator silently assume annual compounding when the product compounds monthly — at higher rates and longer horizons the gap becomes real money.
2. Never divide by zero. If the rate is 0, the contributions formula above divides by r and returns NaN. Guard it:
function safeFutureValue(principal, monthly, annualRate, years) {
if (annualRate === 0) return principal + monthly * years * 12;
return futureValueWithContributions(principal, monthly, annualRate, years);
}
3. Nominal vs effective rate. A bank advertising "5% compounded monthly" actually pays an effective annual rate of (1 + 0.05/12)^12 − 1 ≈ 5.12%. If you compare products, compare effective rates, not nominal ones.
4. Inflation is the silent partner. A 7% nominal return with 2.5% inflation is roughly a 4.4% real return. Your balance in 30 years will look enormous in nominal terms; always ask what it buys in today's money. A quick adjustment: divide the final balance by (1 + inflationRate)^years.
5. Rounding belongs at the display layer. Keep full floating-point precision through every intermediate step and round only when formatting for display (toFixed(2)). Rounding monthly balances before compounding them introduces drift that grows with the horizon.
Visualising the curve
One of the most useful things you can build with this code is a year-by-year table. It turns an abstract formula into something a user can feel:
function yearlyBreakdown(principal, monthly, annualRate, years) {
const rows = [];
let balance = principal;
const r = annualRate / 12;
for (let year = 1; year <= years; year++) {
for (let month = 0; month < 12; month++) {
balance = balance * (1 + r) + monthly;
}
rows.push({ year, balance: Number(balance.toFixed(2)) });
}
return rows;
}
console.table(yearlyBreakdown(0, 200, 0.07, 30).filter(r => r.year % 5 === 0));
Watch the balance column: it grows slowly, then faster, then dramatically. That shape — flat, then steep — is the signature of compounding, and once you have seen it in your own numbers, you never unsee it.
Conclusion
Compound interest is not complicated mathematics. It is one formula, a handful of inputs, and a curve that rewards patience more than brilliance. The developers who understand it can build calculators, dashboards, and planning tools that genuinely help people make better decisions — and can spot a bad fee or an optimistic projection from a mile away.
If you want to play with your own numbers — different rates, contributions, and horizons — try the free Compound Interest Calculator. Change one input at a time and watch the curve move. The best time to start compounding was 30 years ago. The second-best time is the next line of code you write.
Top comments (0)