Loan Amortization Schedule Builder
Compute fixed monthly payments and a full amortization table with Decimal rounding, then total the interest paid.
Code
from dataclasses import dataclass
from decimal import ROUND_HALF_UP, Decimal
CENT = Decimal("0.01")
def quantize(value: Decimal) -> Decimal:
return value.quantize(CENT, rounding=ROUND_HALF_UP)
@dataclass(frozen=True)
class Payment:
period: int
amount: Decimal
interest: Decimal
principal: Decimal
balance: Decimal
def monthly_payment(principal: Decimal, annual_rate: Decimal, months: int) -> Decimal:
if months <= 0:
raise ValueError("months must be positive")
rate = annual_rate / Decimal(12)
if rate == 0:
return quantize(principal / months)
growth = (Decimal(1) + rate) ** months
return quantize(principal * rate * growth / (growth - 1))
def amortization_schedule(
principal: Decimal,
annual_rate: Decimal,
months: int,
) -> list[Payment]:
rate = annual_rate / Decimal(12)
payment = monthly_payment(principal, annual_rate, months)
balance = principal
schedule: list[Payment] = []
for period in range(1, months + 1):
interest = quantize(balance * rate)
principal_part = payment - interest
if period == months:
principal_part = balance
payment = principal_part + interest
balance = quantize(balance - principal_part)
schedule.append(Payment(period, payment, interest, principal_part, balance))
return schedule
def total_interest(schedule: list[Payment]) -> Decimal:
return quantize(sum((row.interest for row in schedule), Decimal(0)))
def main() -> None:
schedule = amortization_schedule(Decimal("25000"), Decimal("0.065"), 24)
print(f"payment: {schedule[0].amount}")
for row in schedule[:3]:
print(row)
print("final balance:", schedule[-1].balance)
print("total interest:", total_interest(schedule))
if __name__ == "__main__":
main()Output
Captured from a sandboxed run of this snippet in the build pipeline.
payment: 1113.66
Payment(period=1, amount=Decimal('1113.66'), interest=Decimal('135.42'), principal=Decimal('978.24'), balance=Decimal('24021.76'))
Payment(period=2, amount=Decimal('1113.66'), interest=Decimal('130.12'), principal=Decimal('983.54'), balance=Decimal('23038.22'))
Payment(period=3, amount=Decimal('1113.66'), interest=Decimal('124.79'), principal=Decimal('988.87'), balance=Decimal('22049.35'))
final balance: 0.00
total interest: 1727.76How the snippet works
Count the classes from and Payment and the functions quantize, monthly_payment, amortization_schedule and total_interest as the entire surface area of Loan Amortization Schedule Builder.
The cycling part of this snippet is for period in range(1, months + 1). Reach for decimal and what you get is dataclass, ROUND_HALF_UP, Decimal — that is the whole API contact area. A guard precedes the ValueError: bad state stops the flow there instead of continuing.
The input schedule is mutated in place — callers that need the original must pass a copy.
What to watch for
decimalmust be installed first — the snippet imports it directly.- The guard
months <= 0short-circuits the flow with an earlyraisewhen its condition holds. scheduleis mutated in place — the value you pass will not be the same afterward.
Applying it
The main signature reads def quantize(value: Decimal) -> Decimal
Look for the payment:, final balance:, total interest: prefixes when the file runs; they tie each line to its step.
The demo flow inside main is amortization_schedule and total_interest — run the file to see it end to end.
Statements that matter
def main() -> None:
schedule = amortization_schedule(Decimal("25000"), Decimal("0.065"), 24)
print(f"payment: {schedule[0].amount}")
for row in schedule[:3]:
print(row)
print("final balance:", schedule[-1].balance)
print("total interest:", total_interest(schedule))
def total_interest(schedule: list[Payment]) -> Decimal:
return quantize(sum((row.interest for row in schedule), Decimal(0)))
def quantize(value: Decimal) -> Decimal:
return value.quantize(CENT, rounding=ROUND_HALF_UP)
Interactive Tools for This Topic
FAQ
- Does `quantize` modify its input?
- Yes — `schedule` is mutated in place. Pass a copy if the caller needs the original preserved.
- What happens if the preconditions do not hold?
- The code refuses to continue: `ValueError` is thrown while the state is still recoverable.
- What does Loan Amortization Schedule Builder print when run?
- Run it as a complete program — the output lines carry labels such as `payment:`, `final balance:`, `total interest:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.