Skip to content

Instantly share code, notes, and snippets.

@larryhou
Created February 28, 2018 05:38
Show Gist options
  • Save larryhou/b42a61d49df69f98e157811f531d1f80 to your computer and use it in GitHub Desktop.
Save larryhou/b42a61d49df69f98e157811f531d1f80 to your computer and use it in GitHub Desktop.
./loan_interest.py -y 30 20 15 10 5 3 2 1 -r 3.0 -g 3

贷款年限: 30年

利率 月供 等额本息利息 等额本金利息 利息差
3.00 0.4216% 51.7775% 45.1250% 6.6525%
3.10 0.4270% 53.7259% 46.6292% 7.0967%
3.20 0.4325% 55.6881% 48.1333% 7.5547%
3.30 0.4380% 57.6639% 49.6375% 8.0264%
3.40 0.4435% 59.6532% 51.1417% 8.5116%
3.50 0.4490% 61.6561% 52.6458% 9.0103%
3.60 0.4546% 63.6723% 54.1500% 9.5223%
3.70 0.4603% 65.7019% 55.6542% 10.0477%
3.80 0.4660% 67.7446% 57.1583% 10.5863%
3.90 0.4717% 69.8006% 58.6625% 11.1381%
4.00 0.4774% 71.8695% 60.1667% 11.7028%
4.10 0.4832% 73.9514% 61.6708% 12.2806%
4.20 0.4890% 76.0462% 63.1750% 12.8712%
4.30 0.4949% 78.1537% 64.6792% 13.4746%
4.40 0.5008% 80.2739% 66.1833% 14.0906%
4.50 0.5067% 82.4067% 67.6875% 14.7192%
4.60 0.5126% 84.5520% 69.1917% 15.3603%
4.70 0.5186% 86.7096% 70.6958% 16.0138%
4.80 0.5247% 88.8795% 72.2000% 16.6795%
4.90 0.5307% 91.0616% 73.7042% 17.3575%
5.00 0.5368% 93.2558% 75.2083% 18.0475%
5.10 0.5429% 95.4619% 76.7125% 18.7494%
5.20 0.5491% 97.6799% 78.2167% 19.4633%
5.30 0.5553% 99.9097% 79.7208% 20.1888%
5.40 0.5615% 102.1511% 81.2250% 20.9261%
5.50 0.5678% 104.4040% 82.7292% 21.6749%
5.60 0.5741% 106.6684% 84.2333% 22.4351%
5.70 0.5804% 108.9442% 85.7375% 23.2067%
5.80 0.5868% 111.2311% 87.2417% 23.9894%
5.90 0.5931% 113.5291% 88.7458% 24.7833%
6.00 0.5996% 115.8382% 90.2500% 25.5882%

贷款年限: 20年

利率 月供 等额本息利息 等额本金利息 利息差
3.00 0.5546% 33.1034% 30.1250% 2.9784%
3.10 0.5596% 34.3081% 31.1292% 3.1789%
3.20 0.5647% 35.5191% 32.1333% 3.3857%
3.30 0.5697% 36.7365% 33.1375% 3.5990%
3.40 0.5748% 37.9602% 34.1417% 3.8186%
3.50 0.5800% 39.1903% 35.1458% 4.0445%
3.60 0.5851% 40.4268% 36.1500% 4.2768%
3.70 0.5903% 41.6695% 37.1542% 4.5153%
3.80 0.5955% 42.9185% 38.1583% 4.7602%
3.90 0.6007% 44.1738% 39.1625% 5.0113%
4.00 0.6060% 45.4353% 40.1667% 5.2686%
4.10 0.6113% 46.7030% 41.1708% 5.5322%
4.20 0.6166% 47.9770% 42.1750% 5.8020%
4.30 0.6219% 49.2571% 43.1792% 6.0779%
4.40 0.6273% 50.5434% 44.1833% 6.3601%
4.50 0.6326% 51.8359% 45.1875% 6.6484%
4.60 0.6381% 53.1344% 46.1917% 6.9427%
4.70 0.6435% 54.4391% 47.1958% 7.2432%
4.80 0.6490% 55.7498% 48.2000% 7.5498%
4.90 0.6544% 57.0666% 49.2042% 7.8624%
5.00 0.6600% 58.3894% 50.2083% 8.1810%
5.10 0.6655% 59.7182% 51.2125% 8.5057%
5.20 0.6711% 61.0530% 52.2167% 8.8363%
5.30 0.6766% 62.3937% 53.2208% 9.1729%
5.40 0.6823% 63.7404% 54.2250% 9.5154%
5.50 0.6879% 65.0930% 55.2292% 9.8638%
5.60 0.6935% 66.4514% 56.2333% 10.2181%
5.70 0.6992% 67.8157% 57.2375% 10.5782%
5.80 0.7049% 69.1858% 58.2417% 10.9442%
5.90 0.7107% 70.5618% 59.2458% 11.3159%
6.00 0.7164% 71.9435% 60.2500% 11.6935%

贷款年限: 15年

利率 月供 等额本息利息 等额本金利息 利息差
3.00 0.6906% 24.3047% 22.6250% 1.6797%
3.10 0.6954% 25.1722% 23.3792% 1.7931%
3.20 0.7002% 26.0434% 24.1333% 1.9101%
3.30 0.7051% 26.9183% 24.8875% 2.0308%
3.40 0.7100% 27.7967% 25.6417% 2.1551%
3.50 0.7149% 28.6789% 26.3958% 2.2830%
3.60 0.7198% 29.5646% 27.1500% 2.4146%
3.70 0.7247% 30.4540% 27.9042% 2.5498%
3.80 0.7297% 31.3470% 28.6583% 2.6887%
3.90 0.7347% 32.2436% 29.4125% 2.8311%
4.00 0.7397% 33.1438% 30.1667% 2.9772%
4.10 0.7447% 34.0476% 30.9208% 3.1268%
4.20 0.7498% 34.9551% 31.6750% 3.2801%
4.30 0.7548% 35.8661% 32.4292% 3.4369%
4.40 0.7599% 36.7806% 33.1833% 3.5973%
4.50 0.7650% 37.6988% 33.9375% 3.7613%
4.60 0.7701% 38.6205% 34.6917% 3.9288%
4.70 0.7753% 39.5458% 35.4458% 4.0999%
4.80 0.7804% 40.4746% 36.2000% 4.2746%
4.90 0.7856% 41.4070% 36.9542% 4.4528%
5.00 0.7908% 42.3429% 37.7083% 4.6345%
5.10 0.7960% 43.2823% 38.4625% 4.8198%
5.20 0.8013% 44.2252% 39.2167% 5.0085%
5.30 0.8065% 45.1716% 39.9708% 5.2008%
5.40 0.8118% 46.1216% 40.7250% 5.3966%
5.50 0.8171% 47.0750% 41.4792% 5.5959%
5.60 0.8224% 48.0319% 42.2333% 5.7986%
5.70 0.8277% 48.9923% 42.9875% 6.0048%
5.80 0.8331% 49.9562% 43.7417% 6.2145%
5.90 0.8385% 50.9235% 44.4958% 6.4276%
6.00 0.8439% 51.8942% 45.2500% 6.6442%

贷款年限: 10年

利率 月供 等额本息利息 等额本金利息 利息差
3.00 0.9656% 15.8729% 15.1250% 0.7479%
3.10 0.9702% 16.4276% 15.6292% 0.7985%
3.20 0.9749% 16.9840% 16.1333% 0.8507%
3.30 0.9795% 17.5421% 16.6375% 0.9046%
3.40 0.9842% 18.1017% 17.1417% 0.9601%
3.50 0.9889% 18.6630% 17.6458% 1.0172%
3.60 0.9935% 19.2260% 18.1500% 1.0760%
3.70 0.9983% 19.7906% 18.6542% 1.1364%
3.80 1.0030% 20.3568% 19.1583% 1.1985%
3.90 1.0077% 20.9247% 19.6625% 1.2622%
4.00 1.0125% 21.4942% 20.1667% 1.3275%
4.10 1.0172% 22.0653% 20.6708% 1.3945%
4.20 1.0220% 22.6381% 21.1750% 1.4631%
4.30 1.0268% 23.2124% 21.6792% 1.5333%
4.40 1.0316% 23.7885% 22.1833% 1.6051%
4.50 1.0364% 24.3661% 22.6875% 1.6786%
4.60 1.0412% 24.9454% 23.1917% 1.7537%
4.70 1.0461% 25.5262% 23.6958% 1.8304%
4.80 1.0509% 26.1087% 24.2000% 1.9087%
4.90 1.0558% 26.6929% 24.7042% 1.9887%
5.00 1.0607% 27.2786% 25.2083% 2.0703%
5.10 1.0655% 27.8660% 25.7125% 2.1535%
5.20 1.0705% 28.4550% 26.2167% 2.2383%
5.30 1.0754% 29.0455% 26.7208% 2.3247%
5.40 1.0803% 29.6377% 27.2250% 2.4127%
5.50 1.0853% 30.2315% 27.7292% 2.5024%
5.60 1.0902% 30.8269% 28.2333% 2.5936%
5.70 1.0952% 31.4240% 28.7375% 2.6865%
5.80 1.1002% 32.0226% 29.2417% 2.7809%
5.90 1.1052% 32.6228% 29.7458% 2.8770%
6.00 1.1102% 33.2246% 30.2500% 2.9746%

贷款年限: 5年

利率 月供 等额本息利息 等额本金利息 利息差
3.00 1.7969% 7.8121% 7.6250% 0.1871%
3.10 1.8013% 8.0790% 7.8792% 0.1998%
3.20 1.8058% 8.3462% 8.1333% 0.2129%
3.30 1.8102% 8.6139% 8.3875% 0.2264%
3.40 1.8147% 8.8820% 8.6417% 0.2403%
3.50 1.8192% 9.1505% 8.8958% 0.2546%
3.60 1.8237% 9.4194% 9.1500% 0.2694%
3.70 1.8281% 9.6887% 9.4042% 0.2845%
3.80 1.8326% 9.9584% 9.6583% 0.3001%
3.90 1.8371% 10.2286% 9.9125% 0.3161%
4.00 1.8417% 10.4991% 10.1667% 0.3325%
4.10 1.8462% 10.7701% 10.4208% 0.3493%
4.20 1.8507% 11.0415% 10.6750% 0.3665%
4.30 1.8552% 11.3133% 10.9292% 0.3841%
4.40 1.8598% 11.5855% 11.1833% 0.4022%
4.50 1.8643% 11.8581% 11.4375% 0.4206%
4.60 1.8689% 12.1311% 11.6917% 0.4395%
4.70 1.8734% 12.4046% 11.9458% 0.4588%
4.80 1.8780% 12.6785% 12.2000% 0.4785%
4.90 1.8825% 12.9527% 12.4542% 0.4986%
5.00 1.8871% 13.2274% 12.7083% 0.5191%
5.10 1.8917% 13.5025% 12.9625% 0.5400%
5.20 1.8963% 13.7780% 13.2167% 0.5613%
5.30 1.9009% 14.0539% 13.4708% 0.5831%
5.40 1.9055% 14.3302% 13.7250% 0.6052%
5.50 1.9101% 14.6070% 13.9792% 0.6278%
5.60 1.9147% 14.8841% 14.2333% 0.6508%
5.70 1.9194% 15.1617% 14.4875% 0.6742%
5.80 1.9240% 15.4396% 14.7417% 0.6980%
5.90 1.9286% 15.7180% 14.9958% 0.7222%
6.00 1.9333% 15.9968% 15.2500% 0.7468%

贷款年限: 3年

利率 月供 等额本息利息 等额本金利息 利息差
3.00 2.9081% 4.6924% 4.6250% 0.0674%
3.10 2.9125% 4.8511% 4.7792% 0.0719%
3.20 2.9169% 5.0100% 4.9333% 0.0766%
3.30 2.9214% 5.1690% 5.0875% 0.0815%
3.40 2.9258% 5.3282% 5.2417% 0.0865%
3.50 2.9302% 5.4875% 5.3958% 0.0917%
3.60 2.9346% 5.6470% 5.5500% 0.0970%
3.70 2.9391% 5.8066% 5.7042% 0.1024%
3.80 2.9435% 5.9664% 5.8583% 0.1080%
3.90 2.9480% 6.1263% 6.0125% 0.1138%
4.00 2.9524% 6.2863% 6.1667% 0.1197%
4.10 2.9568% 6.4466% 6.3208% 0.1257%
4.20 2.9613% 6.6069% 6.4750% 0.1319%
4.30 2.9658% 6.7674% 6.6292% 0.1383%
4.40 2.9702% 6.9281% 6.7833% 0.1448%
4.50 2.9747% 7.0889% 6.9375% 0.1514%
4.60 2.9792% 7.2499% 7.0917% 0.1582%
4.70 2.9836% 7.4110% 7.2458% 0.1652%
4.80 2.9881% 7.5723% 7.4000% 0.1723%
4.90 2.9926% 7.7337% 7.5542% 0.1795%
5.00 2.9971% 7.8952% 7.7083% 0.1869%
5.10 3.0016% 8.0569% 7.8625% 0.1944%
5.20 3.0061% 8.2188% 8.0167% 0.2021%
5.30 3.0106% 8.3808% 8.1708% 0.2100%
5.40 3.0151% 8.5429% 8.3250% 0.2179%
5.50 3.0196% 8.7052% 8.4792% 0.2261%
5.60 3.0241% 8.8677% 8.6333% 0.2344%
5.70 3.0286% 9.0303% 8.7875% 0.2428%
5.80 3.0331% 9.1930% 8.9417% 0.2514%
5.90 3.0377% 9.3559% 9.0958% 0.2601%
6.00 3.0422% 9.5190% 9.2500% 0.2690%

贷款年限: 2年

利率 月供 等额本息利息 等额本金利息 利息差
3.00 4.2981% 3.1549% 3.1250% 0.0299%
3.10 4.3025% 3.2611% 3.2292% 0.0319%
3.20 4.3070% 3.3674% 3.3333% 0.0340%
3.30 4.3114% 3.4737% 3.4375% 0.0362%
3.40 4.3158% 3.5801% 3.5417% 0.0384%
3.50 4.3203% 3.6865% 3.6458% 0.0407%
3.60 4.3247% 3.7931% 3.7500% 0.0431%
3.70 4.3292% 3.8996% 3.8542% 0.0455%
3.80 4.3336% 4.0063% 3.9583% 0.0480%
3.90 4.3380% 4.1130% 4.0625% 0.0505%
4.00 4.3425% 4.2198% 4.1667% 0.0531%
4.10 4.3469% 4.3267% 4.2708% 0.0558%
4.20 4.3514% 4.4336% 4.3750% 0.0586%
4.30 4.3559% 4.5406% 4.4792% 0.0614%
4.40 4.3603% 4.6476% 4.5833% 0.0643%
4.50 4.3648% 4.7547% 4.6875% 0.0672%
4.60 4.3692% 4.8619% 4.7917% 0.0703%
4.70 4.3737% 4.9692% 4.8958% 0.0734%
4.80 4.3782% 5.0765% 5.0000% 0.0765%
4.90 4.3827% 5.1839% 5.1042% 0.0797%
5.00 4.3871% 5.2913% 5.2083% 0.0830%
5.10 4.3916% 5.3989% 5.3125% 0.0864%
5.20 4.3961% 5.5064% 5.4167% 0.0898%
5.30 4.4006% 5.6141% 5.5208% 0.0932%
5.40 4.4051% 5.7218% 5.6250% 0.0968%
5.50 4.4096% 5.8296% 5.7292% 0.1004%
5.60 4.4141% 5.9374% 5.8333% 0.1041%
5.70 4.4186% 6.0453% 5.9375% 0.1078%
5.80 4.4231% 6.1533% 6.0417% 0.1116%
5.90 4.4276% 6.2614% 6.1458% 0.1155%
6.00 4.4321% 6.3695% 6.2500% 0.1195%

贷款年限: 1年

利率 月供 等额本息利息 等额本金利息 利息差
3.00 8.4694% 1.6324% 1.6250% 0.0074%
3.10 8.4739% 1.6871% 1.6792% 0.0079%
3.20 8.4785% 1.7418% 1.7333% 0.0085%
3.30 8.4830% 1.7965% 1.7875% 0.0090%
3.40 8.4876% 1.8512% 1.8417% 0.0096%
3.50 8.4922% 1.9060% 1.8958% 0.0101%
3.60 8.4967% 1.9607% 1.9500% 0.0107%
3.70 8.5013% 2.0155% 2.0042% 0.0113%
3.80 8.5059% 2.0703% 2.0583% 0.0119%
3.90 8.5104% 2.1251% 2.1125% 0.0126%
4.00 8.5150% 2.1799% 2.1667% 0.0132%
4.10 8.5196% 2.2347% 2.2208% 0.0139%
4.20 8.5241% 2.2896% 2.2750% 0.0146%
4.30 8.5287% 2.3444% 2.3292% 0.0153%
4.40 8.5333% 2.3993% 2.3833% 0.0160%
4.50 8.5379% 2.4542% 2.4375% 0.0167%
4.60 8.5424% 2.5091% 2.4917% 0.0175%
4.70 8.5470% 2.5641% 2.5458% 0.0182%
4.80 8.5516% 2.6190% 2.6000% 0.0190%
4.90 8.5562% 2.6740% 2.6542% 0.0198%
5.00 8.5607% 2.7290% 2.7083% 0.0206%
5.10 8.5653% 2.7840% 2.7625% 0.0215%
5.20 8.5699% 2.8390% 2.8167% 0.0223%
5.30 8.5745% 2.8940% 2.8708% 0.0232%
5.40 8.5791% 2.9491% 2.9250% 0.0241%
5.50 8.5837% 3.0041% 2.9792% 0.0250%
5.60 8.5883% 3.0592% 3.0333% 0.0259%
5.70 8.5929% 3.1143% 3.0875% 0.0268%
5.80 8.5975% 3.1694% 3.1417% 0.0278%
5.90 8.6020% 3.2246% 3.1958% 0.0287%
6.00 8.6066% 3.2797% 3.2500% 0.0297%
#!/usr/bin/env python
#encoding:utf-8
import argparse, sys
def caculate_equal_payment_interest(rate, years):
amount, total = 1.0, 0.0
periods, period_rate = years * 12, rate / 100 / 12
payment = amount * period_rate * pow(1 + period_rate, periods) / (pow(1 + period_rate, periods) - 1)
while amount > 0:
interest = amount * period_rate
amount = amount - (payment - interest)
total += interest
# print years, rate, total, payment
return [total, payment]
def caculate_equal_principal_interest(rate, years):
amount, total = 1.0, 0.0
periods, period_rate = years * 12, rate / 100 / 12
delta = amount / periods
while amount > 0:
interest = amount * period_rate
amount -= delta
total += interest
# print years, rate, total
return [total]
def caculate_interest(rate, years):
principal = [x * 100 for x in caculate_equal_principal_interest(rate, years)]
payment = [x * 100 for x in caculate_equal_payment_interest(rate, years)]
print '|%.2f|%.4f%%|%8.4f%%|%8.4f%%|%7.4f%%|'%(rate, payment[1],payment[0],principal[0],payment[0] - principal[0])
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-y', '--years', default = [30], nargs = '+', help = 'number of loan years')
parser.add_argument('-r', '--interest-rate', required = True, help = 'bank loan interest rate')
parser.add_argument('-g', '--interest-rate-range', default = 1.5, help = 'interest rate range list')
data = parser.parse_args(sys.argv[1:])
years_list = [int(x) for x in data.years]
for case_years in years_list:
print '## 贷款年限: %r年'%(case_years)
interest_rate = float(data.interest_rate)
interest_rate_upper = interest_rate + float(data.interest_rate_range)
print '|利率|月供|等额本息利息|等额本金利息|利息差|'
table_format = '|'
for n in range(5):
table_format += ':-:|'
print table_format
while interest_rate < interest_rate_upper:
caculate_interest(interest_rate, case_years)
interest_rate += 0.1
print
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment