from decimal import Decimal
from pickle import GLOBAL
from easygui.boxes.derived_boxes import enterbox
from prettytable import PrettyTable
import copy
#from dealFloat import float_deal # Decimal格式
from decimal import ROUND_HALF_UP # 传入字符串金额得到2尾数的四舍五入金额 (0~4舍,5~9入)
def float_deal(f):
data = Decimal(str(f)).quantize(Decimal('0.00'),ROUND_HALF_UP) #print(data)
return data
import easygui as a
def getnum():
global num
global endnum
global principal
global term_rate
global day_rate
global repayPattern
global line
Msg='请输入贷款信息'
Title='利率(公积金:0.0275(5年内)或0.0325(5年外)商贷:0.0625'
Fields=['贷款多少年(单位/年):','计划还款几年还清(单位/年):','贷款多少钱(单位/万元):', '利率(0.0275-0.0625):']
numlist=a.multenterbox(Msg,Title,Fields, values=())
num=(eval(numlist[0]))*12
endnum=(eval(numlist[1]))*12#提前多少年还款
principal=(eval(numlist[2]))*10000
#print("利率(公积金:0.0275(5年内)或0.0325(5年外)或0.03575(上浮百分之10%),商贷:0.0625")
annual_rate = Decimal(eval(numlist[3]))
#principal = Decimal('500000.00') # 本金
#annual_rate = Decimal('0.0325') # 年化利率
term_rate = annual_rate / Decimal('12') # 月利率
day_rate = annual_rate / Decimal('12') /Decimal('30') # 日利率
repayPattern = {
"MCEP": "等额本金,按月付息", # 借款总利息最低
"MCEI": "等额等息,按月付息"
}
line = PrettyTable(["期数", "还款本息", "当期本金", "当期利息","剩余本金","还款方式"])
#line.align["期数"] = "1" # 以期数字段左对齐
#line.padding_width = 2 # 填充宽度(字段间隔)py
# "等额本金,按月付息"
def get_MCEP(num,endnum):
## 每月本金相同,利息递减,相当于剩余本金的利息,每期利息固定:上一期本金*利率
global MCEPmsg1
global MCEPmsg2
global MCEPmsg3
global MCEPmsg4
global MCEPmsg5
#show = copy.deepcopy(line)
term_prin = float_deal(principal / num) # 每期还本金(2位小数)
prin = principal
total_int = float_deal(0.0)
MCEPzlx=0
for i in range(1, num + 1):
term_int = float_deal(prin * term_rate) # 每期利息固定:上一期本金*利率
# 如果是最后一期,还款本金为上一期的剩余本金;
if i == num:
term_prin = prin
if i < endnum+1:
MCEPzlx=MCEPzlx+term_int
MCEPsybj=prin-term_prin
if i == 1:
MCEPterm_amt=term_prin + term_int
term_amt = term_prin + term_int
prin = prin - term_prin # 剩余本金=上期剩余本金-当期还本金
#show.add_row([i, term_amt, term_prin, term_int, prin,'等额本金'])
total_int = float_deal(total_int + term_int)
MCEPmsg1=('等额本金总利息为:'+str(total_int))
MCEPmsg2=("提前还款总利息:"+str(MCEPzlx))
MCEPmsg3=("剩余本金:"+str(MCEPsybj))
MCEPmsg4=("月供还款:"+str(MCEPterm_amt))
#计算总花费
zhf1= principal+MCEPzlx
MCEPmsg5=("总共支付金额:"+str(zhf1))
#print(show)
# "MCEI": "等额本息,按月付息"
def get_MCEI(num,endnum):
# 本金+利息保持相同,本金逐月递增,利息逐月递减,月还款数不变。
global MCEImsg1
global MCEImsg2
global MCEIterm_amt
global MCEImsg3
global MCEImsg4
global MCEImsg5
#show = copy.deepcopy(line)
term_amt = float_deal((principal*term_rate*(1+term_rate)**num)/((1+term_rate)**num-1)) # 每期还款总额 **是幂运算
prin = principal
total_int = float_deal(0.0)
MCEIzlx=0
for i in range(1, num + 1):
term_int = float_deal(prin * term_rate) # 每期利息计算固定:上一期本金*利率
term_prin = term_amt - term_int# 如果是最后一期,还款本金为上一期的剩余本金;
if i < endnum+1:
MCEIzlx=MCEIzlx+term_int
MCEIsybj=prin - term_prin
if i == num:
term_prin = prin
if i == 1:
MCEIterm_amt=term_prin + term_int
term_amt = term_prin + term_int
prin = prin - term_prin # 剩余本金=上期剩余本金-当期还本金
#show.add_row([i, term_amt, term_prin, term_int,prin,'等额等息'])
total_int = total_int + term_int
MCEImsg1=('等额本息总利息为:'+str(total_int))
MCEImsg2=("提前还款总利息:"+str(MCEIzlx))
MCEImsg3=("剩余本金:"+str(MCEIsybj))
MCEImsg4=("月还款:"+str(MCEIterm_amt))
#计算总花费
zhf2= principal+MCEIzlx
MCEImsg5=("总共支付金额:"+str(zhf2))
#print(show)
if __name__ == '__main__':
#print("================分===============割===============线===============")
try:
getnum()
get_MCEP(num,endnum)
#print("================分===============割===============线===============") # 等额本金,按月付息
get_MCEI(num,endnum)
a.msgbox(msg='总贷款:'+str(principal)+'\n\n'+MCEPmsg1+'\n'+MCEPmsg2+'\n'+MCEPmsg5+'\n'+MCEPmsg4+'\n'+MCEPmsg3+'\n\n'+MCEImsg1+'\n'+MCEImsg2+'\n'+MCEImsg5+'\n'+MCEImsg4+'\n'+MCEImsg3, title='结果', ok_button='OK ', image=None, root=None) # 等额本息,按月付息
except Exception as e:
print(e)