【问题标题】:Django How to calculate monthly interest over an objects value?Django 如何计算对象价值的每月利息?
【发布时间】:2016-01-28 21:59:02
【问题描述】:

我正在寻找我的问题的答案,但只得到了这个:

这不是一个真正的 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)

这会给你:

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

如果正确,我将把这个公式放在哪里?在哪个 Django 文件中?

【问题讨论】:

    标签: python mysql django oop instance


    【解决方案1】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-08
      • 1970-01-01
      • 2021-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多