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

标签 python mysql django oop instance

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

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/

相关文章:

python - 是否可以控制从 matplotlib 颜色图中检索哪些颜色?

python - 使用 spacy 删除停用词

mysql - mysql中如何统计某个字符串出现的次数

django - 单独的服务器来服务 django 应用程序及其内容?

database - Django - 这些查询集会被缓存吗?

python - 通过最小化迭代次数来改进代码

python - 从 0 个固定装置安装了 0 个对象,没有正确读取 FIXTURE_DIRS

MySQL SUM() 返回虚假 (?) 值

php - 将 BIG mysql 表导出到 JSON

python - 使用 PIP 安装 PIL 时出错 - Django、Python