From 8f3dadb7178a07ebadf782507cf2af5995321c71 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Tue, 2 Jan 2024 19:43:21 +0100 Subject: Initial commit --- mortgage.html | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ mortgage.js | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mortgage.py | 55 +++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 4 files changed, 198 insertions(+) create mode 100644 mortgage.html create mode 100644 mortgage.js create mode 100644 mortgage.py create mode 100644 requirements.txt diff --git a/mortgage.html b/mortgage.html new file mode 100644 index 0000000..07de66b --- /dev/null +++ b/mortgage.html @@ -0,0 +1,68 @@ +<!DOCTYPE html> +<html> + +<head> + <title>Mortgage Calculator</title> + <style> + #parameters { + margin-top: 2em; + display: grid; + grid-template-columns: 10em min-content; + column-gap: 1em; + row-gap: 0.5em; + } + #parameters div { + text-align: right; + } + #parameters input { + width: 12em; + text-align: right; + } + #results { + margin-top: 2em; + } + #results-table { + border: 1px solid; + border-collapse: collapse; + } + #results-table th { + padding-top: 0.5em; + padding-bottom: 0.5em; + } + #results-table th, td { + text-align: center; + padding-left: 1em; + padding-right: 1em; + } + #results-table tr:nth-child(even){ + background-color: #f2f2f2; + } + #results-table tr:hover {background-color: #ddd;} + </style> +</head> + +<body> + <h1>Mortgage Calculator</h1> + <div id="parameters"> + <div>Principal</div> + <div><input id="principal" type="number" value="100000" onchange="update()"></div> + <div>Annual Interest (%)</div> + <div><input id="interest" type="number" value="5.0" onchange="update()"></div> + <div>Installments</div> + <div><input id="installments" type="number" value="300" onchange="update()"></div> + </div> + <div id="results"> + <table id="results-table"> + <tr> + <th>Time</th> + <th>Outstanding Debt</th> + <th>Monthly Installment</th> + <th>Interest Paid</th> + <th>Amount Amortised</th> + </tr> + </table> + </div> + <script type="text/javascript" src="mortgage.js"></script> +</body> + +</html> diff --git a/mortgage.js b/mortgage.js new file mode 100644 index 0000000..ffc1b1d --- /dev/null +++ b/mortgage.js @@ -0,0 +1,74 @@ +/// Round up the given number two the nearest two decimal places. +function round(x) { + return Math.ceil(x*100)/100; +} + +/// Format the float as a string using two decimal places. +function fmt(x) { + return round(x).toFixed(2); +} + +/// Clear all rows in the table except for the first row (header). +function clear_table(table) { + while (table.rows.length > 1) { + table.deleteRow(1); + } +} + +/// Insert the values into a new row in the table. +function insert_row(table, values) { + var row = table.insertRow(table.rows.length); + values.forEach((value) => { + row.insertCell(row.cells.length).innerHTML = value; + }); +} + +/// Compute the mortgage. +function compute_mortgage(principal, annual_interest, installments, table) { + const p = principal; + const i = annual_interest / 100 / 12; // Monthly equivalent. + const n = installments; + + const monthly_installment = p * (i * (1 + i)**n) / ((1 + i)**n - 1); + + var debt = principal; + var total_interest = 0; + var total_amortised = 0; + + clear_table(table); + + for (k = 0; k < n; ++k) { + const interest = i * debt; + const amortised = monthly_installment - interest; + + const values = [k.toString(), fmt(debt), fmt(monthly_installment), fmt(interest), fmt(amortised)]; + insert_row(table, values); + + total_interest += interest; + total_amortised += amortised; + debt -= amortised; + } + + const total_interest_pct = total_interest / principal * 100; + + insert_row(table, ["", "", "", "", ""]); // Separator. + + insert_row(table, ["Total", "", "Amount Paid", "Interest Paid", "Amount Amortised"]); + insert_row(table, [ + "", + "", + fmt(monthly_installment * n), + fmt(total_interest) + " (" + fmt(total_interest_pct) + "%)", + fmt(total_amortised)]); +} + +function update() { + const principal = parseFloat(document.getElementById("principal").value); + const interest = parseFloat(document.getElementById("interest").value); + const installments = parseInt(document.getElementById("installments").value); + const results = document.getElementById("results-table"); + + compute_mortgage(principal, interest, installments, results); +} + +update(); diff --git a/mortgage.py b/mortgage.py new file mode 100644 index 0000000..3b9b958 --- /dev/null +++ b/mortgage.py @@ -0,0 +1,55 @@ +import argparse +import math +import sys +from tabulate import tabulate, SEPARATING_LINE + + +def calc_payments(principal, annual_interest, installments): + p = principal + i = annual_interest / 100 / 12 # Monthly equivalent. + n = installments + + def fmt(x): + return f"{math.ceil(x*100)/100:.2f}" + + table = [["Time", "Outstanding Debt", "Monthly Installment", "Interest Paid", "Amount Amortised"]] + + monthly_installment = p * (i * (1 + i)**n) / ((1 + i)**n - 1) + debt = principal + total_interest = 0 + total_amortised = 0 + + for k in range(n): + interest = i * debt + amortised = monthly_installment - interest + table.append([k] + [fmt(x) for x in [debt, monthly_installment, interest, amortised]]) + + total_interest += interest + total_amortised += amortised + debt -= amortised + + total_interest_pct = total_interest / principal * 100 + + table.append(SEPARATING_LINE) + table.append(["Total", "", "Amount Paid", "Interest Paid", "Amount Amortised"]) + table.append(["", ""] + [ + fmt(monthly_installment * n), + fmt(total_interest) + f" ({fmt(total_interest_pct)}%)", + fmt(total_amortised)]) + return table + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("-p", "--principal", type=float, default=100000, help="Principal") + parser.add_argument("-i", "--interest", type=float, default=5, help="Annual interest %") + parser.add_argument("-n", "--installments", type=int, default=300, help="Number of installments") + args = parser.parse_args() + + table = calc_payments(args.principal, args.interest, args.installments) + print(tabulate(table)) + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..a5d5159 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +tabulate -- cgit v1.2.3