简体   繁体   English

如何计算复利并使用Django在浏览器中显示?

[英]How do I calculate compound interest and display it in a browser using Django?

I am trying to calculate interest using django. 我正在尝试使用Django计算利息。

In my models: 在我的模型中:

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) My goal is to calculate the monthly interest. 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)我的目标是计算每月利息。 I need to iterate through each months', write the value to the database, and display the results of the table 我需要遍历每个月,将值写入数据库,并显示表的结果

How do I calculate principal * rate * months for each month in django? 如何计算Django每个月的本金*利率*月? How do I these values in an html table? 如何在html表中使用这些值?

This isn't really a Django problem, and your questions is a little unclear. 这不是一个真正的Django问题,您的问题还不清楚。 You basically need to apply the compound interest formula in python to an instance of this model: 您基本上需要将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)

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM