网站首页 二手房 > 综合生活 > 正文
背景
网上有很多在线版本的房贷计算器,但是想要不能对其结果进行自定义分析,所以就手动使用go语言写了一个房贷计算器,也可以通过C/C++/java编程语言将代码进行修改成自己的代码。
功能
支持等额本息/等额本金
支持计算每一期还款金额/利息/本金/还有多少本金未还/总的利息等
参考
等额本息理论: https://zhuanlan.zhihu.com/p/390581715
代码
package mainimport ( "fmt" "github.com/olekukonko/tablewriter" "math" "os" "strconv")// RepayStatEveryMonth 每个月还款统计type RepayStatEveryMonth struct { RepayAmount float64 // 每个月还款金额 RepayOfInterest float64 // 每个月还款的利息部分 RepayOfCorpus float64 // 每个月还款的本金部分 RemainCorpus float64 // 每个月还款之后还有多少本金未还 MonthId int // 第几个月份}// EqualityCorpus 等额本金type EqualityCorpus struct { InterestOfMonth float64 // 月利息 InterestOfYear float64 // 年利息 RepayMonths int // 还款月数 RepayCorpusEveryMonth float64 // 每个月还的本金数目 LoadAmount float64 // 贷款金额 TotalRepayOfInterest float64 // 还款中利息部分 RepayDetail []RepayStatEveryMonth // 每个月还款细节}func (e *EqualityCorpus) calc() { e.RepayCorpusEveryMonth = e.LoadAmount / float64(e.RepayMonths) e.TotalRepayOfInterest = 0 for i := 1; i <= e.RepayMonths; i++ { interest := (e.LoadAmount - float64(i-1)*e.RepayCorpusEveryMonth) * e.InterestOfMonth e.TotalRepayOfInterest += interest e.RepayDetail = append(e.RepayDetail, RepayStatEveryMonth{ RepayAmount: e.RepayCorpusEveryMonth + interest, RepayOfInterest: interest, RepayOfCorpus: e.RepayCorpusEveryMonth, RemainCorpus: float64(e.RepayMonths-i) * e.RepayCorpusEveryMonth, MonthId: i, }) }}func NewEqualityCorpus(loadAmount, yearInterest float64, repayMonths int) *EqualityCorpus { return &EqualityCorpus{ InterestOfMonth: yearInterest / 100.0 / 12.0, InterestOfYear: yearInterest / 100.0, RepayMonths: repayMonths, LoadAmount: loadAmount, TotalRepayOfInterest: 0, RepayDetail: make([]RepayStatEveryMonth, 0), }}// EqualityCorpusAndInterest 等额本息type EqualityCorpusAndInterest struct { InterestOfMonth float64 // 月利息 InterestOfYear float64 // 年利息 RepayMonths int // 还款月数 RepayAmountEveryMonth float64 // 每个月还款金额 LoadAmount float64 // 贷款金额 TotalRepayOfInterest float64 // 还款中利息部分 RepayDetail []RepayStatEveryMonth // 每个月还款细节}func NewEqualityCorpusAndInterest(loadAmount, yearInterest float64, repayMonths int) *EqualityCorpusAndInterest { return &EqualityCorpusAndInterest{ InterestOfMonth: yearInterest / 100.0 / 12.0, InterestOfYear: yearInterest / 100.0, RepayMonths: repayMonths, RepayAmountEveryMonth: 0, LoadAmount: loadAmount, TotalRepayOfInterest: 0, RepayDetail: make([]RepayStatEveryMonth, 0), }}// 等额本息 参考: https://zhuanlan.zhihu.com/p/390581715func (e *EqualityCorpusAndInterest) calc() { // 计算每个月还款金额 X := func(A, R, n float64) float64 { x1 := A * R * math.Pow(1+R, n) x2 := math.Pow(1+R, n) - 1 return x1 / x2 } // 每个月还款之后,还有多少本金未还 Q := func(A, R, x float64, k int) float64 { x1 := math.Pow(1+R, float64(k)) x2 := 1 - x1 q := A*x1 + x*x2/R return q } // 每个月还的利息 Rn := func(q, R float64) float64 { return q * R } // 每个月还的本金 Cn := func(x, r float64) float64 { return x - r } e.RepayAmountEveryMonth = X(e.LoadAmount, e.InterestOfMonth, float64(e.RepayMonths)) e.TotalRepayOfInterest = 0.0 for i := 1; i <= e.RepayMonths; i++ { t := RepayStatEveryMonth{ RepayAmount: e.RepayAmountEveryMonth, RepayOfInterest: 0, RepayOfCorpus: 0, RemainCorpus: Q(e.LoadAmount, e.InterestOfMonth, e.RepayAmountEveryMonth, i), MonthId: i, } // caution: 这里是i-1,不是i,表示的是上个月的未还金额在一个月的时间内产生的利息 q := Q(e.LoadAmount, e.InterestOfMonth, e.RepayAmountEveryMonth, i-1) t.RepayOfInterest = Rn(q, e.InterestOfMonth) t.RepayOfCorpus = Cn(e.RepayAmountEveryMonth, t.RepayOfInterest) e.RepayDetail = append(e.RepayDetail, t) e.TotalRepayOfInterest += t.RepayOfInterest }}func compareSummary(ec *EqualityCorpus, eci *EqualityCorpusAndInterest) { table := tablewriter.NewWriter(os.Stdout) table.SetHeader([]string{"还款方式", "期数", "贷款金额", "总利息", "利息/金额比例"}) data := [][]string{ { "等额本息", strconv.FormatInt(int64(eci.RepayMonths), 10), strconv.FormatInt(int64(eci.LoadAmount), 10), strconv.FormatFloat(eci.TotalRepayOfInterest, 'f', 2, 64), strconv.FormatFloat(100*eci.TotalRepayOfInterest/eci.LoadAmount, 'f', 2, 64) + "%", }, { "等额本金", strconv.FormatInt(int64(ec.RepayMonths), 10), strconv.FormatInt(int64(ec.LoadAmount), 10), strconv.FormatFloat(ec.TotalRepayOfInterest, 'f', 2, 64), strconv.FormatFloat(100*ec.TotalRepayOfInterest/ec.LoadAmount, 'f', 2, 64) + "%", }, } for _, row := range data { table.Append(row) } table.Render()}func compareDetail(ec *EqualityCorpus, eci *EqualityCorpusAndInterest, months int) { if months > len(ec.RepayDetail) || months > len(eci.RepayDetail) { fmt.Println("invalid param") return } table := tablewriter.NewWriter(os.Stdout) table.SetHeader([]string{ "月份", "方式", "还款金额", "利息", "本金", "利息/金额比例", "方式", "还款金额", "利息", "本金", "利息/金额比例", }) for i := 0; i < months; i++ { d := []string{ strconv.FormatInt(int64(i), 10), "等额本息", strconv.FormatFloat(eci.RepayDetail[i].RepayOfCorpus+eci.RepayDetail[i].RepayOfInterest, 'f', 2, 64), strconv.FormatFloat(eci.RepayDetail[i].RepayOfInterest, 'f', 2, 64), strconv.FormatFloat(eci.RepayDetail[i].RepayOfCorpus, 'f', 2, 64), strconv.FormatFloat(eci.RepayDetail[i].RepayOfInterest*100/eci.RepayDetail[i].RepayOfCorpus, 'f', 2, 64) + "%", "等额本金", strconv.FormatFloat(ec.RepayDetail[i].RepayOfCorpus+ec.RepayDetail[i].RepayOfInterest, 'f', 2, 64), strconv.FormatFloat(ec.RepayDetail[i].RepayOfInterest, 'f', 2, 64), strconv.FormatFloat(ec.RepayDetail[i].RepayOfCorpus, 'f', 2, 64), strconv.FormatFloat(ec.RepayDetail[i].RepayOfInterest*100/ec.RepayDetail[i].RepayOfCorpus, 'f', 2, 64) + "%", } table.Append(d) } table.Render()}func main() { loadAmount, yearInterest, months := float64(1000000), 4.75, 60 x := NewEqualityCorpus(loadAmount, yearInterest, months) x.calc() y := NewEqualityCorpusAndInterest(loadAmount, yearInterest, months) y.calc() compareSummary(x, y) compareDetail(x, y, months)}
执行结果
+----------+------+----------+-----------+---------------+| 还款方式 | 期数 | 贷款金额 | 总利息 | 利息/金额比例 |+----------+------+----------+-----------+---------------+| 等额本息 | 60 | 1000000 | 125414.72 | 12.54% || 等额本金 | 60 | 1000000 | 120729.17 | 12.07% |+----------+------+----------+-----------+---------------++------+----------+----------+---------+----------+---------------+----------+----------+---------+----------+---------------+| 月份 | 方式 | 还款金额 | 利息 | 本金 | 利息/金额比例 | 方式 | 还款金额 | 利息 | 本金 | 利息/金额比例 |+------+----------+----------+---------+----------+---------------+----------+----------+---------+----------+---------------+| 0 | 等额本息 | 18756.91 | 3958.33 | 14798.58 | 26.75% | 等额本金 | 20625.00 | 3958.33 | 16666.67 | 23.75% || 1 | 等额本息 | 18756.91 | 3899.76 | 14857.16 | 26.25% | 等额本金 | 20559.03 | 3892.36 | 16666.67 | 23.35% || 2 | 等额本息 | 18756.91 | 3840.95 | 14915.97 | 25.75% | 等额本金 | 20493.06 | 3826.39 | 16666.67 | 22.96% || 3 | 等额本息 | 18756.91 | 3781.90 | 14975.01 | 25.25% | 等额本金 | 20427.08 | 3760.42 | 16666.67 | 22.56% || 4 | 等额本息 | 18756.91 | 3722.63 | 15034.28 | 24.76% | 等额本金 | 20361.11 | 3694.44 | 16666.67 | 22.17% || 5 | 等额本息 | 18756.91 | 3663.12 | 15093.80 | 24.27% | 等额本金 | 20295.14 | 3628.47 | 16666.67 | 21.77% || 6 | 等额本息 | 18756.91 | 3603.37 | 15153.54 | 23.78% | 等额本金 | 20229.17 | 3562.50 | 16666.67 | 21.38% || 7 | 等额本息 | 18756.91 | 3543.39 | 15213.52 | 23.29% | 等额本金 | 20163.19 | 3496.53 | 16666.67 | 20.98% || 8 | 等额本息 | 18756.91 | 3483.17 | 15273.74 | 22.80% | 等额本金 | 20097.22 | 3430.56 | 16666.67 | 20.58% || 9 | 等额本息 | 18756.91 | 3422.71 | 15334.20 | 22.32% | 等额本金 | 20031.25 | 3364.58 | 16666.67 | 2
版权说明: 本文由用户上传,如有侵权请联系删除!
猜你喜欢:
- 2023-11-25 上海40天天气预报(上海40天天气预报最新查询)
- 2023-11-25 安新房屋出租最新消息(宁国房屋出租最新消息)
- 2023-11-25 上海邮编(上海邮编浦东新区)
- 2023-11-25 赶集网怎么没有租房信息了(赶集网租房)
- 2023-11-25 浙江劳务市场招工信息(东阳劳务市场招工)
- 2023-11-25 今日国内油价最新消息(今日国内油价最新消息热议)
- 2023-11-25 玫瑰小镇手机版下载(玫瑰小镇手机怎么登录)
- 2023-11-25 河北人才网(河北人才网官网入口登录)
最新文章:
- 2023-11-25 深圳住房公积金提取(深圳住房公积金提取条件及额度)
- 2023-11-25 北京房子价格最新消息(北京房子利率最新消息)
- 2023-11-25 广州番禺区房子出租(番禺区升降车出租)
- 2023-11-25 上海厂房出租58同城网(武义厂房出租58同城网)
- 2023-11-25 北京南锣鼓巷(北京南锣鼓巷简介)
- 2023-11-25 2021年9月房产税新规(南京房产税2021年9月1
- 2023-11-25 万达集团股票(万达集团股票多少钱一股)
- 2023-11-25 唐山房产信息网查询(唐山房产信息网查诿)
- 2023-11-25 昆明未来5年房价
- 2023-11-25 猪头三个人房源网
- 2023-11-25 青岛房子降价最新消息(武汉房子降价最新消息)
- 2023-11-25 北京平房区(北京平房区交警队)
- 2023-11-25 宝华二手房房价(句容宝华二手房房价)
- 2023-11-25 上海40天天气预报(上海40天天气预报最新查询)
- 2023-11-25 芷江二手房出售信息(芷江二手房出售信息网)
- 2023-11-25 工业园区厂房出售(安岳县工业园区厂房出售)