2015-08-21 52 views
0

我想用django来计算兴趣。如何计算复合兴趣并使用Django在浏览器中显示它?

在我的模型:

class Account(models.Model): principal = models.DecimalField("pricipal", max_digits=15, decimal_places=6) rate = models.DecimalField("interest rate", max_digits=5, decimal_places=5) months = models.IntegerField("number of months", default=0) 我的目标是计算,每月利息。我需要遍历每个月',将值写入数据库,并显示表结果

如何计算django中每个月的principal * rate *个月? 我如何在HTML表中的这些值?

回答

0

这不是一个真正的Django问题,你的问题有点不清楚。你基本上需要在Python应用复利公式这种模式的一个实例:

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) 

这会给你:

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) 
+0

谢谢你,让我有我的Python脚本,我怎么走来自模型的输入,计算利息,然后将结果发布到数据库并在视图中显示摊销计划? – toddkovalsky