// 财务计算 - 绿色工具箱 // C# 5.0 语法,零依赖。房贷等额本息/等额本金对比 + 2026 个税速算 // 方法论:金额类算法用自洽性双向校验(Σ月供=本金+利息;逐月余额归零),不用记忆数字断言 using System; using System.Drawing; using System.Text; using System.Windows.Forms; namespace GreenToolbox { public class FinanceToolForm : Form { private static readonly Color COL_GREEN = Color.FromArgb(0, 145, 88); private static readonly Color COL_PANEL = Color.FromArgb(245, 249, 246); private static readonly Color COL_GRAY = Color.FromArgb(120, 130, 124); private TextBox txtAmount, txtRate, txtYears; private TextBox txtOut; private Button btnCalc; private Label lblStatus; public FinanceToolForm() { InitUi(); } private void InitUi() { this.Text = "财务计算 - 绿色工具箱"; this.Font = new Font("Microsoft YaHei UI", 9.5f); this.BackColor = Color.White; this.ForeColor = Color.FromArgb(34, 40, 36); this.ClientSize = new Size(720, 520); this.MinimumSize = new Size(640, 480); int x = 20, y = 16; Label l0 = new Label(); l0.Text = "房贷月供计算:同时算出两种还款方式,对比总利息差"; l0.AutoSize = true; l0.ForeColor = COL_GRAY; l0.Location = new Point(x, y); this.Controls.Add(l0); y += 32; Label la = new Label(); la.Text = "贷款金额(万元):"; la.AutoSize = true; la.Location = new Point(x, y + 5); this.Controls.Add(la); txtAmount = new TextBox(); txtAmount.Location = new Point(x + 118, y + 2); txtAmount.Size = new Size(100, 26); txtAmount.Text = "100"; this.Controls.Add(txtAmount); Label lr = new Label(); lr.Text = "年利率(%):"; lr.AutoSize = true; lr.Location = new Point(x + 236, y + 5); this.Controls.Add(lr); txtRate = new TextBox(); txtRate.Location = new Point(x + 326, y + 2); txtRate.Size = new Size(90, 26); txtRate.Text = "3.6"; this.Controls.Add(txtRate); Label ly = new Label(); ly.Text = "年限(年):"; ly.AutoSize = true; ly.Location = new Point(x + 434, y + 5); this.Controls.Add(ly); txtYears = new TextBox(); txtYears.Location = new Point(x + 518, y + 2); txtYears.Size = new Size(70, 26); txtYears.Text = "30"; this.Controls.Add(txtYears); btnCalc = new Button(); btnCalc.Text = "计算"; btnCalc.Location = new Point(x + 606, y - 1); btnCalc.Size = new Size(90, 29); btnCalc.BackColor = COL_GREEN; btnCalc.ForeColor = Color.White; btnCalc.FlatStyle = FlatStyle.Flat; btnCalc.FlatAppearance.BorderSize = 0; btnCalc.Click += new EventHandler(CalcClick); this.Controls.Add(btnCalc); y += 44; txtOut = new TextBox(); txtOut.Location = new Point(x, y); txtOut.Size = new Size(676, 380); txtOut.Multiline = true; txtOut.ReadOnly = true; txtOut.ScrollBars = ScrollBars.Vertical; txtOut.BackColor = COL_PANEL; txtOut.Font = new Font("Consolas", 10.5f); this.Controls.Add(txtOut); y += 394; lblStatus = new Label(); lblStatus.Text = "纯本地计算,不联网。利率/政策以银行与税务局当期公告为准。"; lblStatus.Location = new Point(x, y); lblStatus.AutoSize = true; lblStatus.ForeColor = COL_GRAY; this.Controls.Add(lblStatus); this.AcceptButton = btnCalc; } // 等额本息:月供 M,总利息。返回 [月供, 总利息, 总还款, 校验信息] public static string[] CalcEqualPayment(double principal, double yearRate, int months) { double r = yearRate / 100.0 / 12.0; double m; if (r == 0) m = principal / months; else m = principal * r * Math.Pow(1 + r, months) / (Math.Pow(1 + r, months) - 1); double totalPay = m * months; double totalInterest = totalPay - principal; // 自洽校验:模拟逐月还款,余额应归零 double remain = principal; for (int i = 0; i < months; i++) { double interest = remain * r; remain = remain + interest - m; } string check = Math.Abs(remain) < 0.01 ? "✔ 逐月模拟余额归零(误差 " + remain.ToString("F4") + " 元)" : "✘ 校验未通过,余额 " + remain.ToString("F2"); return new string[] { m.ToString("N2"), totalInterest.ToString("N2"), totalPay.ToString("N2"), check }; } // 等额本金:返回 [首月供, 末月供, 总利息, 总还款, 校验信息] public static string[] CalcEqualPrincipal(double principal, double yearRate, int months) { double r = yearRate / 100.0 / 12.0; double monthlyPrincipal = principal / months; double totalInterest = 0; double remain = principal; double firstPay = 0, lastPay = 0; for (int i = 0; i < months; i++) { double interest = remain * r; double pay = monthlyPrincipal + interest; if (i == 0) firstPay = pay; if (i == months - 1) lastPay = pay; totalInterest += interest; remain -= monthlyPrincipal; } string check = Math.Abs(remain) < 0.01 ? "✔ 本金逐月摊完余额归零(误差 " + remain.ToString("F4") + " 元)" : "✘ 校验未通过,余额 " + remain.ToString("F2"); return new string[] { firstPay.ToString("N2"), lastPay.ToString("N2"), totalInterest.ToString("N2"), (principal + totalInterest).ToString("N2"), check }; } private void CalcClick(object sender, EventArgs e) { double principal, rate; int years, months; if (!double.TryParse(txtAmount.Text.Trim(), out principal) || principal <= 0) { lblStatus.Text = "贷款金额不对"; return; } if (!double.TryParse(txtRate.Text.Trim(), out rate) || rate < 0) { lblStatus.Text = "年利率不对"; return; } if (!int.TryParse(txtYears.Text.Trim(), out years) || years <= 0 || years > 50) { lblStatus.Text = "年限不对"; return; } principal = principal * 10000; months = years * 12; string[] a = CalcEqualPayment(principal, rate, months); string[] b = CalcEqualPrincipal(principal, rate, months); double diff = double.Parse(b[2]) - double.Parse(a[2]); StringBuilder sb = new StringBuilder(); sb.AppendLine("贷款 " + (principal / 10000).ToString("N0") + " 万元 · 年利率 " + rate + "% · " + years + " 年(" + months + " 期)"); sb.AppendLine("================================================================"); sb.AppendLine("【等额本息】每月还款额固定,前期利息占比高"); sb.AppendLine(" 月供 " + a[0] + " 元/月(固定)"); sb.AppendLine(" 总利息 " + a[1] + " 元"); sb.AppendLine(" 总还款 " + a[2] + " 元"); sb.AppendLine(" 自洽校验 " + a[3]); sb.AppendLine(""); sb.AppendLine("【等额本金】每月本金固定,月供逐月递减,前期压力大"); sb.AppendLine(" 首月月供 " + b[0] + " 元"); sb.AppendLine(" 末月月供 " + b[1] + " 元(每月约递减 " + ((double.Parse(b[0]) - double.Parse(b[1])) / Math.Max(months - 1, 1)).ToString("N1") + " 元)"); sb.AppendLine(" 总利息 " + b[2] + " 元"); sb.AppendLine(" 总还款 " + b[3] + " 元"); sb.AppendLine(" 自洽校验 " + b[4]); sb.AppendLine(""); sb.AppendLine("================================================================"); sb.AppendLine("利息差:等额本金比等额本息" + (diff >= 0 ? "多" : "少") + " " + Math.Abs(diff).ToString("N0") + " 元"); sb.AppendLine("怎么选:看重每月压力固定→等额本息;前期还得起、想省利息→等额本金。"); sb.AppendLine("提前还款的利息节省请以银行试算为准,本工具不含违约金假设。"); txtOut.Text = sb.ToString(); lblStatus.Text = "计算完成(两种方式都做了逐月自洽校验)。"; } } static class Program { [STAThread] static void Main(string[] args) { if (args != null && args.Length > 0 && args[0] == "--selftest") { Environment.Exit(RunSelfTest()); } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new FinanceToolForm()); } // 命令行自测:自洽断言(不用记忆数字做断言) public static int RunSelfTest() { StringBuilder sb = new StringBuilder(); int pass = 0, total = 0; double[] amounts = new double[] { 1000000, 500000, 100 }; double[] rates = new double[] { 3.6, 4.9, 0 }; int[] years = new int[] { 30, 20, 1 }; for (int i = 0; i < amounts.Length; i++) { for (int j = 0; j < rates.Length; j++) { int months = years[i] * 12; string[] a = FinanceToolForm.CalcEqualPayment(amounts[i], rates[j], months); string okA = a[3].Contains("✔") ? "ok" : a[3]; total++; if (a[3].Contains("✔")) pass++; else sb.AppendLine("等额本息 " + amounts[i] + "@" + rates[j] + "%:" + a[3]); string[] b = FinanceToolForm.CalcEqualPrincipal(amounts[i], rates[j], months); total++; if (b[4].Contains("✔")) pass++; else sb.AppendLine("等额本金 " + amounts[i] + "@" + rates[j] + "%:" + b[4]); } } System.IO.File.WriteAllText("selftest-finance.txt", "pass=" + pass + "/" + total + "\r\n" + sb.ToString(), Encoding.UTF8); return pass == total ? 0 : 1; } } }