Andrey Consalter
Andrey Consalter

Reputation: 43

Django How to calculate monthly interest over an objects value?

I search for an answer to my problem and only got this:

This isn't really a Django problem, and your questions is a little unclear. You basically need to apply the compound interest formula in python to an instance of this model:

account = Account.objects.get(pk=<something>)
calc_interest = lambda value: value * account.rate
amount = account.principal
for i in xrange(12):
    interest = calc_interest(amount)
    amount += interest
    print 'month {}: {} ({} interest)'.format(i, amount, interest)

This will give you:

month 0: 1050.0 (50.0 interest) month 1: 1102.5 (52.5 interest) month 2: 1157.625 (55.125 interest) month 3: 1215.50625 (57.88125 interest) month 4: 1276.2815625 (60.7753125 interest) month 5: 1340.09564062 (63.814078125 interest) month 6: 1407.10042266 (67.0047820312 interest) month 7: 1477.45544379 (70.3550211328 interest) month 8: 1551.32821598 (73.8727721895 interest) month 9: 1628.89462678 (77.5664107989 interest) month 10: 1710.33935812 (81.4447313389 interest) month 11: 1795.85632602 (85.5169679058 interest)

If that's correct, where to I put this formula? In which Django file?

Upvotes: 1

Views: 421

Answers (1)

Todor
Todor

Reputation: 16020

This can be a public method of your Account model. Maybe something like this:

class Account(models.Model):

    #..other methods and properties here

    def montly_interest(self):
        amount = self.principal
        calc_interest = lambda value: value * self.rate
        montly = []
        for i in xrange(12):
            interest = calc_interest(amount)
            amount += interest
            montly.append((i, amount, interest))
        return montly

Upvotes: 1

Related Questions