random tips on coding, ops, ...

房贷还款计算的python程序

贴一个几年前用python写的房贷还款计算程序,:)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/python
# -*- coding: utf-8 -*-

import os, sys

if __name__ == "__main__":
    pay = ( # 月还款额
                                                              5000, 5000, 5000, # 2007
        5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, # 2008
        )
    year  = 2007       # 还款起始年
    month = 10         # 还款起始月
    day   = 15         # 还款日
    total = 600000.00; # 贷款总额
    # 从还款起始上一年开始的年利率
    # 如果还款起始月是一月,需要上一年的利率
    rate =  ( 0, 5.04, 5.22, 3.87, 3.87 )
    capital = 0.       # 本金
    t_pay = 0.         # 总本金
    interest = 0.      # 利息
    t_interest = 0.    # 总利息

    p_counter = 0;
    r_counter = 1;

    result = []
    result.append(" 时间      还款       本金   利息    待还本金  以还本金 总利息")
    while (total > 0.009):
        if (month == 1) : # 计算跨年度利率
            rate_m = (rate[r_counter - 1] * (31 - day) + rate[r_counter]* (day - 1)) /30.
        else:
            rate_m = rate[r_counter]
        rate_m = rate_m * 0.01 / 12.         # 月利率 = 年利率 / 12.
        interest = total * rate_m;           # 当月利息
        t_interest += interest;              # 总利息
        capital = pay[p_counter] - interest; # 当月本金
        if (total <= capital) :              # 当月本金超过余额,调整
            capital = interest/rate_m
        t_pay += capital                     # 以还总本金
        total -= capital;                    # 剩余本金
        if (month == 1 ) :
            result.append(" 时间      还款       本金   利息    待还本金  以还本金   总利息")
        result.append("%d%02d %9.2f %9.2f %7.2f %9.2f  %9.2f %9.2f" % (year, month, capital + interest, capital, interest, total, t_pay, t_interest))
        if (p_counter < (len(pay) - 1)) : p_counter = p_counter + 1
        if (month == 12):
            year = year + 1
            month = 1
            if (r_counter < (len(rate) - 1)) : r_counter = r_counter + 1
        else:
            month = month + 1
    if os.name == "posix":
        print "\n".join(result)
    else:
        print "\n".join(result).decode('utf-8')
    sys.exit(0)

comments powered by Disqus