【问题标题】:How do I calculate compound interest and display it in a browser using Django?如何使用 Django 计算复利并在浏览器中显示?
【发布时间】:2015-08-21 19:40:16
【问题描述】:

我正在尝试使用 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 中计算每个月的本金 * 利率 * 月数? 我如何在 html 表格中获取这些值?

【问题讨论】:

    标签: django python-2.7 django-models django-forms django-views


    【解决方案1】:

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

    【讨论】:

    • 谢谢,我有我的 python 脚本,我如何从模型中获取输入,计算利息,然后将结果发布到 db 并在视图中显示摊销计划?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-11
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-06
    相关资源
    最近更新 更多