gpt4 book ai didi

python - Django 如何计算对象值的月利息?

转载 作者:行者123 更新时间:2023-11-28 23:38:33 25 4
gpt4 key购买 nike

我搜索我的问题的答案,只得到了这个:

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)

如果那是正确的,我应该把这个公式放在哪里?在哪个 Django 文件中?

最佳答案

这可以是您的 Account 模型的公共(public)方法。也许是这样的:

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

关于python - Django 如何计算对象值的月利息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35072901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com