Calculate mortgage amortization and total interest


This example shows using math functions to compute an amortization schedule for a mortgage, as well as total interest. It shows how much interest vs principal is paid each month.

Create a directory for this application and create the application itself:
mkdir mortgage
cd mortgage
rim -k mortgage

Create file 'amort.rim' with the code:
cat << 'EOF' > amort.rim
%% /amort/print-schedule public
    get-param principal type double, num_payments type number, rate type double

    set-double monthly_rate = rate / 100 / 12
    set-double monthly_payment=principal * (monthly_rate * pow(1 + monthly_rate, num_payments)) / (pow(1 + monthly_rate, num_payments) - 1)
    set-double balance = principal

    print-out "Amortization Schedule:" new-line
    print-format "Payment #\tPayment\t\tPrincipal\tInterest\tBalance\n"

    set-double tot_interest
    start-loop use i start-with 1 repeat num_payments
        set-double interest = balance * monthly_rate
        set-double principal_payment = monthly_payment - interest
        set-double balance = balance - principal_payment
        set-double tot_interest = tot_interest + interest

        print-format "%12ld\t%.2f\t\t%.2f\t\t%.2f\t\t%.2f\n", i, monthly_payment, principal_payment, interest, balance
    end-loop

    print-format "\nInterest paid: %.2f\n", tot_interest
%%
EOF

Make the application:
rim -q

Run it:
rim -r --req="/amort/print-schedule/principal=350000/rate=6/num_payments=360" --exec

The result is (use --silent-header to not display the header, i.e. Content-Type etc.).
Content-Type: text/html;charset=utf-8
Cache-Control: max-age=0, no-cache
Pragma: no-cache
Status: 200 OK

Amortization Schedule:
Payment #	Payment		Principal	Interest	Balance
           1	2098.43		348.43		1750.00		349651.57
           2	2098.43		350.17		1748.26		349301.40
           3	2098.43		351.92		1746.51		348949.48
           4	2098.43		353.68		1744.75		348595.80
           5	2098.43		355.45		1742.98		348240.36
           6	2098.43		357.23		1741.20		347883.13
           7	2098.43		359.01		1739.42		347524.12
           8	2098.43		360.81		1737.62		347163.31
           9	2098.43		362.61		1735.82		346800.70
          10	2098.43		364.42		1734.00		346436.28
          11	2098.43		366.25		1732.18		346070.04
          12	2098.43		368.08		1730.35		345701.96
          13	2098.43		369.92		1728.51		345332.04
          14	2098.43		371.77		1726.66		344960.28
          15	2098.43		373.63		1724.80		344586.65
          16	2098.43		375.49		1722.93		344211.16
          17	2098.43		377.37		1721.06		343833.79
          18	2098.43		379.26		1719.17		343454.53
          19	2098.43		381.15		1717.27		343073.37
          20	2098.43		383.06		1715.37		342690.31
          21	2098.43		384.98		1713.45		342305.34
          22	2098.43		386.90		1711.53		341918.44
          23	2098.43		388.83		1709.59		341529.60
          24	2098.43		390.78		1707.65		341138.82
          25	2098.43		392.73		1705.69		340746.09
          26	2098.43		394.70		1703.73		340351.40
          27	2098.43		396.67		1701.76		339954.73
          28	2098.43		398.65		1699.77		339556.07
          29	2098.43		400.65		1697.78		339155.43
          30	2098.43		402.65		1695.78		338752.78
          31	2098.43		404.66		1693.76		338348.11
          32	2098.43		406.69		1691.74		337941.43
          33	2098.43		408.72		1689.71		337532.71
          34	2098.43		410.76		1687.66		337121.94
          35	2098.43		412.82		1685.61		336709.13
          36	2098.43		414.88		1683.55		336294.25
          37	2098.43		416.96		1681.47		335877.29
          38	2098.43		419.04		1679.39		335458.25
          39	2098.43		421.14		1677.29		335037.11
          40	2098.43		423.24		1675.19		334613.87
          41	2098.43		425.36		1673.07		334188.51
          42	2098.43		427.48		1670.94		333761.03
          43	2098.43		429.62		1668.81		333331.41
          44	2098.43		431.77		1666.66		332899.64
          45	2098.43		433.93		1664.50		332465.71
          46	2098.43		436.10		1662.33		332029.61
          47	2098.43		438.28		1660.15		331591.33
          48	2098.43		440.47		1657.96		331150.86
          49	2098.43		442.67		1655.75		330708.19
          50	2098.43		444.89		1653.54		330263.30
          51	2098.43		447.11		1651.32		329816.19
          52	2098.43		449.35		1649.08		329366.85
          53	2098.43		451.59		1646.83		328915.26
          54	2098.43		453.85		1644.58		328461.41
          55	2098.43		456.12		1642.31		328005.29
          56	2098.43		458.40		1640.03		327546.89
          57	2098.43		460.69		1637.73		327086.19
          58	2098.43		463.00		1635.43		326623.20
          59	2098.43		465.31		1633.12		326157.89
          60	2098.43		467.64		1630.79		325690.25
          61	2098.43		469.98		1628.45		325220.27
         ...
         ...
         ...
         341	2098.43		1899.21		199.22		37944.50
         342	2098.43		1908.70		189.72		36035.80
         343	2098.43		1918.25		180.18		34117.55
         344	2098.43		1927.84		170.59		32189.71
         345	2098.43		1937.48		160.95		30252.23
         346	2098.43		1947.17		151.26		28305.07
         347	2098.43		1956.90		141.53		26348.16
         348	2098.43		1966.69		131.74		24381.48
         349	2098.43		1976.52		121.91		22404.96
         350	2098.43		1986.40		112.02		20418.56
         351	2098.43		1996.33		102.09		18422.22
         352	2098.43		2006.32		92.11		16415.91
         353	2098.43		2016.35		82.08		14399.56
         354	2098.43		2026.43		72.00		12373.13
         355	2098.43		2036.56		61.87		10336.57
         356	2098.43		2046.74		51.68		8289.83
         357	2098.43		2056.98		41.45		6232.85
         358	2098.43		2067.26		31.16		4165.59
         359	2098.43		2077.60		20.83		2087.99
         360	2098.43		2087.99		10.44		-0.00

Interest paid: 405433.66

You can of course run this program as a service:
mrim -w 1 mortgage

And then make a request to this service:
rim -r --req="/amort/print-schedule/principal=350000/rate=6/num_payments=360" --exec --service --app="/mortgage"

This will produce the same result, but the actual execution will happen in the server process you started with mrim above.

And finally, this service can serve as a back-end for a web server (Apache, Nginx, HAProxy and pretty much any other). An example of doing this can be found here.


Copyright (c) 2019-2025 Gliim LLC. All contents on this web site is "AS IS" without warranties or guarantees of any kind.