2015-08-21 83 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