用go语言开发的软件 (用go语言重写java)

背景

网上有很多在线版本的房贷计算器,但是想要不能对其结果进行自定义分析,所以就手动使用go语言写了一个房贷计算器,也可以通过C/C++/java编程语言将代码进行修改成自己的代码。

功能

  • 支持等额本息/等额本金
  • 支持计算每一期还款金额/利息/本金/还有多少本金未还/总的利息等

参考

  • 等额本息理论: https://zhuanlan.zhihu.com/p/390581715

代码

package main

import (
	"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/390581715
func (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 | 20.19%        |
|   10 | 等额本息 | 18756.91 | 3362.01 | 15394.90 | 21.84%        | 等额本金 | 19965.28 | 3298.61 | 16666.67 | 19.79%        |
|   11 | 等额本息 | 18756.91 | 3301.07 | 15455.84 | 21.36%        | 等额本金 | 19899.31 | 3232.64 | 16666.67 | 19.40%        |
|   12 | 等额本息 | 18756.91 | 3239.89 | 15517.02 | 20.88%        | 等额本金 | 19833.33 | 3166.67 | 16666.67 | 19.00%        |
|   13 | 等额本息 | 18756.91 | 3178.47 | 15578.44 | 20.40%        | 等额本金 | 19767.36 | 3100.69 | 16666.67 | 18.60%        |
|   14 | 等额本息 | 18756.91 | 3116.81 | 15640.10 | 19.93%        | 等额本金 | 19701.39 | 3034.72 | 16666.67 | 18.21%        |
|   15 | 等额本息 | 18756.91 | 3054.90 | 15702.01 | 19.46%        | 等额本金 | 19635.42 | 2968.75 | 16666.67 | 17.81%        |
|   16 | 等额本息 | 18756.91 | 2992.74 | 15764.17 | 18.98%        | 等额本金 | 19569.44 | 2902.78 | 16666.67 | 17.42%        |
|   17 | 等额本息 | 18756.91 | 2930.35 | 15826.57 | 18.52%        | 等额本金 | 19503.47 | 2836.81 | 16666.67 | 17.02%        |
|   18 | 等额本息 | 18756.91 | 2867.70 | 15889.21 | 18.05%        | 等额本金 | 19437.50 | 2770.83 | 16666.67 | 16.62%        |
|   19 | 等额本息 | 18756.91 | 2804.80 | 15952.11 | 17.58%        | 等额本金 | 19371.53 | 2704.86 | 16666.67 | 16.23%        |
|   20 | 等额本息 | 18756.91 | 2741.66 | 16015.25 | 17.12%        | 等额本金 | 19305.56 | 2638.89 | 16666.67 | 15.83%        |
|   21 | 等额本息 | 18756.91 | 2678.27 | 16078.65 | 16.66%        | 等额本金 | 19239.58 | 2572.92 | 16666.67 | 15.44%        |
|   22 | 等额本息 | 18756.91 | 2614.62 | 16142.29 | 16.20%        | 等额本金 | 19173.61 | 2506.94 | 16666.67 | 15.04%        |
|   23 | 等额本息 | 18756.91 | 2550.72 | 16206.19 | 15.74%        | 等额本金 | 19107.64 | 2440.97 | 16666.67 | 14.65%        |
|   24 | 等额本息 | 18756.91 | 2486.58 | 16270.34 | 15.28%        | 等额本金 | 19041.67 | 2375.00 | 16666.67 | 14.25%        |
|   25 | 等额本息 | 18756.91 | 2422.17 | 16334.74 | 14.83%        | 等额本金 | 18975.69 | 2309.03 | 16666.67 | 13.85%        |
|   26 | 等额本息 | 18756.91 | 2357.51 | 16399.40 | 14.38%        | 等额本金 | 18909.72 | 2243.06 | 16666.67 | 13.46%        |
|   27 | 等额本息 | 18756.91 | 2292.60 | 16464.31 | 13.92%        | 等额本金 | 18843.75 | 2177.08 | 16666.67 | 13.06%        |
|   28 | 等额本息 | 18756.91 | 2227.43 | 16529.48 | 13.48%        | 等额本金 | 18777.78 | 2111.11 | 16666.67 | 12.67%        |
|   29 | 等额本息 | 18756.91 | 2162.00 | 16594.91 | 13.03%        | 等额本金 | 18711.81 | 2045.14 | 16666.67 | 12.27%        |
|   30 | 等额本息 | 18756.91 | 2096.31 | 16660.60 | 12.58%        | 等额本金 | 18645.83 | 1979.17 | 16666.67 | 11.87%        |
|   31 | 等额本息 | 18756.91 | 2030.36 | 16726.55 | 12.14%        | 等额本金 | 18579.86 | 1913.19 | 16666.67 | 11.48%        |
|   32 | 等额本息 | 18756.91 | 1964.15 | 16792.76 | 11.70%        | 等额本金 | 18513.89 | 1847.22 | 16666.67 | 11.08%        |
|   33 | 等额本息 | 18756.91 | 1897.68 | 16859.23 | 11.26%        | 等额本金 | 18447.92 | 1781.25 | 16666.67 | 10.69%        |
|   34 | 等额本息 | 18756.91 | 1830.95 | 16925.96 | 10.82%        | 等额本金 | 18381.94 | 1715.28 | 16666.67 | 10.29%        |
|   35 | 等额本息 | 18756.91 | 1763.95 | 16992.96 | 10.38%        | 等额本金 | 18315.97 | 1649.31 | 16666.67 | 9.90%         |
|   36 | 等额本息 | 18756.91 | 1696.68 | 17060.23 | 9.95%         | 等额本金 | 18250.00 | 1583.33 | 16666.67 | 9.50%         |
|   37 | 等额本息 | 18756.91 | 1629.15 | 17127.76 | 9.51%         | 等额本金 | 18184.03 | 1517.36 | 16666.67 | 9.10%         |
|   38 | 等额本息 | 18756.91 | 1561.36 | 17195.55 | 9.08%         | 等额本金 | 18118.06 | 1451.39 | 16666.67 | 8.71%         |
|   39 | 等额本息 | 18756.91 | 1493.29 | 17263.62 | 8.65%         | 等额本金 | 18052.08 | 1385.42 | 16666.67 | 8.31%         |
|   40 | 等额本息 | 18756.91 | 1424.96 | 17331.96 | 8.22%         | 等额本金 | 17986.11 | 1319.44 | 16666.67 | 7.92%         |
|   41 | 等额本息 | 18756.91 | 1356.35 | 17400.56 | 7.79%         | 等额本金 | 17920.14 | 1253.47 | 16666.67 | 7.52%         |
|   42 | 等额本息 | 18756.91 | 1287.47 | 17469.44 | 7.37%         | 等额本金 | 17854.17 | 1187.50 | 16666.67 | 7.12%         |
|   43 | 等额本息 | 18756.91 | 1218.32 | 17538.59 | 6.95%         | 等额本金 | 17788.19 | 1121.53 | 16666.67 | 6.73%         |
|   44 | 等额本息 | 18756.91 | 1148.90 | 17608.01 | 6.52%         | 等额本金 | 17722.22 | 1055.56 | 16666.67 | 6.33%         |
|   45 | 等额本息 | 18756.91 | 1079.20 | 17677.71 | 6.10%         | 等额本金 | 17656.25 |  989.58 | 16666.67 | 5.94%         |
|   46 | 等额本息 | 18756.91 | 1009.23 | 17747.68 | 5.69%         | 等额本金 | 17590.28 |  923.61 | 16666.67 | 5.54%         |
|   47 | 等额本息 | 18756.91 |  938.98 | 17817.94 | 5.27%         | 等额本金 | 17524.31 |  857.64 | 16666.67 | 5.15%         |
|   48 | 等额本息 | 18756.91 |  868.45 | 17888.46 | 4.85%         | 等额本金 | 17458.33 |  791.67 | 16666.67 | 4.75%         |
|   49 | 等额本息 | 18756.91 |  797.64 | 17959.27 | 4.44%         | 等额本金 | 17392.36 |  725.69 | 16666.67 | 4.35%         |
|   50 | 等额本息 | 18756.91 |  726.55 | 18030.36 | 4.03%         | 等额本金 | 17326.39 |  659.72 | 16666.67 | 3.96%         |
|   51 | 等额本息 | 18756.91 |  655.18 | 18101.73 | 3.62%         | 等额本金 | 17260.42 |  593.75 | 16666.67 | 3.56%         |
|   52 | 等额本息 | 18756.91 |  583.53 | 18173.39 | 3.21%         | 等额本金 | 17194.44 |  527.78 | 16666.67 | 3.17%         |
|   53 | 等额本息 | 18756.91 |  511.59 | 18245.32 | 2.80%         | 等额本金 | 17128.47 |  461.81 | 16666.67 | 2.77%         |
|   54 | 等额本息 | 18756.91 |  439.37 | 18317.54 | 2.40%         | 等额本金 | 17062.50 |  395.83 | 16666.67 | 2.37%         |
|   55 | 等额本息 | 18756.91 |  366.86 | 18390.05 | 1.99%         | 等额本金 | 16996.53 |  329.86 | 16666.67 | 1.98%         |
|   56 | 等额本息 | 18756.91 |  294.07 | 18462.84 | 1.59%         | 等额本金 | 16930.56 |  263.89 | 16666.67 | 1.58%         |
|   57 | 等额本息 | 18756.91 |  220.99 | 18535.93 | 1.19%         | 等额本金 | 16864.58 |  197.92 | 16666.67 | 1.19%         |
|   58 | 等额本息 | 18756.91 |  147.62 | 18609.30 | 0.79%         | 等额本金 | 16798.61 |  131.94 | 16666.67 | 0.79%         |
|   59 | 等额本息 | 18756.91 |   73.95 | 18682.96 | 0.40%         | 等额本金 | 16732.64 |   65.97 | 16666.67 | 0.40%         |
+------+----------+----------+---------+----------+---------------+----------+----------+---------+----------+---------------+

Process finished with the exit code 0

校验

  • 可以通过现有的在线房贷计算器进行校验:http://daikuan.jsq886.com