Calculation of loan repayments

Frank Millman frank at chagford.com
Mon May 24 09:01:10 EDT 2004


Hi all

This is fairly trivial, but I had to work it out for the application I
am writing, so I thought I would share it.

It is a function to calculate loan repayments in the same way that a
financial calculator does. You pass it the loan amount, the interest
rate, and the number of periods. It returns the finance charges, the
repayments required, and a list showing how to 'earn' the finance
charges over the period of the loan.

The magic is in line 4. Don't ask me to explain it - I copied it from
the user manual for my Sharp financial calculator. The rest should be
self explanatory.

Hope this is of interest to someone.

Frank Millman

---------------------------------------------------------------------

def calc(pv,i,n):
    if i:
        i /= 1200.0  # reduce to monthly rate
        inst = pv / ((1-(1+i)**-n)/i)  # instalment
    else:
        inst = pv / float(n)
    tot = int(inst*n)  # do not round up
    fc = tot - pv  # finance charges
    pmt = int(round(inst))  # monthly payment
    if pmt * n == tot:
        pmts = [(n,pmt)]
    else:
        pmts = [(n-1,pmt),(1,tot-(pmt*(n-1)))]
    fcs = []  # list of finance charges to earn per period
    bal = pv
    for x in xrange(n-1):
        fcp = int(round(bal*i))  # fc for period
        fcs.append(fcp)
        bal = bal + fcp - pmt
    fcs.append(fc - sum(fcs))  # force balance the last one
    return fc,pmts,fcs

pv = 10000  # loan amount ($100.00)
i = 10  # interest rate
n = 12  # number of periods

print 'pv =',pv,' int =',i,' per =',n
fc,pmts,fcs =  calc(pv,i,n)
print fc
print pmts
print fcs



More information about the Python-list mailing list