Compound Interest Calculator — Python source
Project investment growth with any compounding frequency, scheduled contributions and inflation adjustment. Yearly breakdown, growth curve and CSV export — runs entirely in your browser.
This is the Python implementation — the same logic the interactive tool runs, in a shareable, citable form.
"""
compound-interest-calculator - compound growth projection.
Display snippet: ports the core project() math from the TypeScript lib
(src/lib/interest.ts). The full projection (contributions, inflation,
per-year rows) lives in the TypeScript/Go sources.
Semantics: monthly simulation with the monthly rate set to the exact
equivalent of the stated compounding frequency — (1 + r/n)**(n/12) - 1 —
so the effective annual rate is preserved for every supported frequency.
Returns the final balance rounded to 2 decimals, or None on invalid input
(negative principal, non-integer years, unsupported frequency,
non-finite values). Standard library only.
"""
import math
VALID_COMPOUNDS = {1, 2, 4, 12, 365}
def round2(n):
"""Round to 2 decimals, half-up to mirror the TS Math.round."""
return math.floor(n * 100 + 0.5) / 100
def project(principal, rate_pct, years, compounds_per_year):
values = (principal, rate_pct, years, compounds_per_year)
if not all(math.isfinite(v) for v in values):
return None
if principal < 0 or years < 0 or not isinstance(years, int):
return None
if compounds_per_year not in VALID_COMPOUNDS:
return None
monthly_rate = (
(1 + rate_pct / 100 / compounds_per_year) ** (compounds_per_year / 12) - 1
)
balance = principal
for _ in range(years * 12):
balance *= 1 + monthly_rate
return round2(balance)
# Example:
# project(10000, 5, 10, 12) -> 16470.09
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →