哪位帮忙优化一下买房的银行按揭贷款算法

kmbirdman 2006-03-02 05:55:41
private void debx(double dk,double ll,int ys) //等额本息计算
{
//dk贷款额,ll年利率,贷款ys月数
try
{
this.dtb = DateTime.Now; //用于统计所用时间的
this.Cursor = Cursors.WaitCursor;
double dylx, dybj, bxhj, lxhj, bjhj, sybj; //当月利息,当月本金,利息合计,本金合计,剩余本金
double yll = ll / 12 / 100; //月利率
dybj = 0; lxhj = 0; bjhj = 0;
double myhk = dk * yll * Math.Pow((1 + yll), ys) / (Math.Pow((1 + yll), ys) - 1);//每月还款
myhk = Math.Round(myhk, 2);

//生成excel文件
StreamWriter excelfile = null;
string filename = "等额" + System.DateTime.Now.Date.ToString("yyyy-MM-dd") + ".csv";
if (File.Exists(filename)) File.Delete(filename);
excelfile = new StreamWriter(filename, false, System.Text.Encoding.Default);
string filehead = "按揭贷款" + dk.ToString() + "元," + this.nud.Value.ToString() + "年,利率" + ll.ToString() + "," + this.hkfs.SelectedItem.ToString() + "还款,每月还款" + myhk.ToString() + "元,情况如下:\r\n";
excelfile.WriteLine(filehead);
string columnhead = "期数,当月还款,当月利息,当月本金,利息合计,本金合计,本息合计,剩余本金";
excelfile.WriteLine(columnhead);

//在窗口的文本框内显示结果
this.textBox1.Text = filehead;
this.textBox1.Text += "\r\n\r\n";
this.textBox1.Text += "期数".PadLeft(4) + ": |" + "当月利息".PadRight(8) + "|当月本金".PadRight(9);
this.textBox1.Text += "|利息合计".PadRight(9) + "|本金合计".PadRight(9) + "|本息合计".PadRight(9) + "|剩余本金".PadRight(10) + "\r\n";
for (int i = 1; i <= ys; i++)
{
dylx = Math.Round((dk - bjhj) * yll, 2);
lxhj += dylx;
dybj = myhk - dylx;
bjhj += dybj;
bxhj = lxhj + bjhj;
sybj = Math.Round((dk - bjhj), 2);
this.textBox1.Text += i.ToString().PadLeft(6) + ": |" + dylx.ToString().PadRight(12) + "|" + dybj.ToString().PadRight(12) + "|" + lxhj.ToString().PadRight(12) + "|";
this.textBox1.Text += bjhj.ToString().PadRight(12) + "|" + bxhj.ToString().PadRight(12) + "|" + sybj.ToString().PadRight(12) + "\r\n";

excelfile.WriteLine("{0},{1},{2},{3},{4},{5},{6},{7}", i, myhk, dylx, dybj, lxhj, bjhj, bxhj, sybj);//写稿到excel文件
Application.DoEvents();
}
excelfile.Close();
this.textBox1.Text += "\r\n";

//生成txt文件
StreamWriter txtfile = null;
string txtname = "等额" + System.DateTime.Now.Date.ToString("yyyy-MM-dd") + ".txt";
if (File.Exists(txtname)) File.Delete(txtname);
txtfile = new StreamWriter(txtname, false, System.Text.Encoding.Default);
txtfile.WriteLine(this.textBox1.Text);
txtfile.Close();

this.dte = DateTime.Now;//用于统计所用时间的
TimeSpan ts = this.dte - this.dtb;
Int64 totalhm = (Int64)ts.TotalMilliseconds;//整个计算所用毫秒数
Int64 m = (totalhm - totalhm % 1000) / 1000;//转换成秒数
Int64 hm = totalhm % 1000;//不足1秒的毫秒数
this.tssl.Text = "计算完成,用时"+m.ToString()+"."+hm.ToString()+"秒。";
this.Cursor = Cursors.Default;
}
catch(Exception err)
{
this.tssl.Text = err.Message;
}
}

//P4 2.8G 超线程那种,1G内存
//感觉算得太慢,算一个30年的,要70秒左右,把输出excel和txt文件那一部分去掉,算一个20年的也要15秒左右,实在受不了,我觉得应该在一、两秒之内就应该搞定了。
//不知道哪里有问题,请知道的帮忙改进一下,需要源代码的请留下email。
...全文
498 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
kmbirdman 2006-03-06
  • 打赏
  • 举报
回复
终于找到原因了,是这两行:
this.textBox1.Text += i.ToString().PadLeft(6) + ": |" + dylx.ToString().PadRight(12) + "|" + dybj.ToString().PadRight(12) + "|" + lxhj.ToString().PadRight(12) + "|";
this.textBox1.Text += bjhj.ToString().PadRight(12) + "|" + bxhj.ToString().PadRight(12) + "|" + sybj.ToString().PadRight(12) + "\r\n";

声明一个string,把结果存在string里,计算结束后再this.textBox1.Text = ...;
OK,全部在1秒内完成。
cattleknife 2006-03-04
  • 打赏
  • 举报
回复
和钱有关的干嘛不用decimal类型啊,就这样直接上去吧,说不定Math类中的Pow方法就是优化过的算法,要比你拍脑袋想出来的算法要强呢,我想微软不会在Pow中用乘方这种东西吧
kmbirdman 2006-03-03
  • 打赏
  • 举报
回复
目前只有这个公式:
double myhk = dk * yll * Math.Pow((1 + yll), ys) / (Math.Pow((1 + yll), ys) - 1);
算每月还多少款的,其它就不知道了,不用循环感觉有难度,因为每个月还款额里的本金和利息所占的比例都不相同。
高手提点意见啊。
kmbirdman 2006-03-03
  • 打赏
  • 举报
回复
Up一下。
长江支流 2006-03-02
  • 打赏
  • 举报
回复
哈哈,我以为是问银行 的东东
jrl5365 2006-03-02
  • 打赏
  • 举报
回复
做个记号
wanhuibing 2006-03-02
  • 打赏
  • 举报
回复
新手学习
wanhuibing@sohu.com
babay2008 2006-03-02
  • 打赏
  • 举报
回复
帮你顶一下
xshxiong@163.com
wxdl1981 2006-03-02
  • 打赏
  • 举报
回复
最好的办法是先求出公式,然后写代码.
就像从1 加到 100000
如果用For循环也会很慢的.

公式会加快运算速度

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧