Semua artikel

How I check every number on a compound interest calculator

A calculator website has one job: give the right number. If the number is off by a cent, nobody...

KalkNest (Open-Source)5 min read1 kali dilihat
How I check every number on a compound interest calculator

A calculator website has one job: give the right number. If the number is off by a cent, nobody notices, until someone compares it with their bank's figure and stops trusting everything else on the site. So when I built KalkNest, a compound interest calculator, I spent more time on checking the numbers than on the interface. Here's how it works.

1. One pure engine, no surprises

All the math lives in a set of pure TypeScript functions: no database, no network, no React, no clock. The same code runs on the server (it computes every number on generated pages) and in the browser (the calculator recomputes on each keystroke), so the two can't disagree.

One detail that matters: instead of looping in fractions of a year, the engine splits the year into whole "ticks" (the least common multiple of 12, the compounding frequency and the deposit frequency). Every deposit and every interest credit lands on an exact tick, so there's no floating-point drift from adding 1/52 of a year 520 times.

2. Two independent answer keys

A test that compares your code to your own formula only proves the code does what you wrote. So I use sources I didn't write.

Excel. Excel's FV function is the standard check for this kind of math. One test re-implements FV and EFFECT with Excel's sign rules and compares them with the engine (simplified here):

const FV = (r: number, n: number, pmt: number, pv: number, type = 0) =>
  -(pv * (1 + r) ** n + pmt * (1 + r * type) * (((1 + r) ** n - 1) / r));
expect(FV(0.05 / 12, 12 * 20, -200, -10000)).toBeCloseTo(fv("plan"), 6);
expect(FV(0.05 / 12, 12 * 20, -200, -10000, 1)).toBeCloseTo(fv("early"), 6);

For $10,000 plus $200 a month at 5% compounded monthly for 20 years, =FV(0.05/12, 240, -200, -10000) gives $109,333.14. With deposits at the start of each month (type = 1), $109,675.66. The engine matches both to six decimals. This test covers four examples and EFFECT.

A reference calculator. Excel's FV assumes deposits and compounding happen at the same frequency, and real calculators don't. So the larger suite compares the engine with results I recorded from an established online calculator: 13 scenarios, 43 tests. Eleven scenarios match to the cent. Two match within a few cents, because that calculator rounds each withdrawal. Examples include $5,000 at 5%, monthly compounding, $100 added at the end of each month for 10 years ($23,763.28), the same with deposits at the start ($23,827.98), and daily compounding on a 360-day versus a 365-day year.

I also keep the cases where my engine deliberately differs, as tests. There are three documented differences. The reference calculator turns weekly deposits into one month-end amount, while my engine credits each deposit on its own date, so the results differ slightly ($25,084.25 vs $25,057.29). In two withdrawal cases where withdrawals exceed the balance, the reference lets the balance go negative, while mine stops at zero and reports the month the money ran out. A documented difference is a decision. An undocumented one is a bug.

3. Rounding: keep precision, round only at display

The engine keeps full precision. Rounding happens only when numbers are shown, and then it has to be consistent. Early on, my tables rounded each column separately, so a row's interest plus deposits could differ from the change in balance by a cent. The fix: interest shown is the change in the rounded balance minus net contributions, so every row and total adds up to the cent.

4. Test the sentences, not just the numbers

The page text never contains a typed number. Copy refers to values like {{example.futureValue}}, and the engine fills them in at render time, so a number in a sentence can't drift from the calculator. A missing placeholder fails the build.

Claims in words get tested too. A sentence like "a higher rate beats compounding more often" becomes an assertion:

expect(fv("annual")).toBeLessThan(fv("monthly"));
expect(fv("monthly")).toBeLessThan(fv("daily"));
expect(fv("higher")).toBeGreaterThan(fv("daily"));

This caught a mistake in one of my own guides. A sentence claimed that doubling the late saver's deposit would leave them slightly behind the early saver at 6%. The test showed they end slightly ahead. I fixed the text before publishing.

5. Where the safety net stops

As of this writing, the project has 512 unit and database tests, 234 browser tests (50 of them accessibility checks), and Lighthouse runs on 13 pages in CI. That doesn't make it perfect. It makes mistakes cheaper to find, and it means every change has to explain itself to a test.

The method matters more than the project: pick an answer key you didn't write, keep the deliberate differences on record, round at display, and make the prose prove itself.

If you want to try the calculator, it's at kalknest.com, and the reference scenarios are described on its methodology page.


Sumber artikel open-source oleh KalkNest di DEV Community — Baca artikel asli. Topik: #webdev #testing #typescript #nextjs

Sukai & bagikan artikel ini

Ditulis & Dikurasi Oleh

KalkNest (Open-Source)

Software Engineer yang berfokus pada Next.js, TypeScript, arsitektur backend skalabel, Cloud, dan Applied AI.

Semua artikel

Komentar