新写的代码,12.3数值转成中文大写,其实有点过于复杂了,
convert.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class convert : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//把输入的数字转为数字型
string strnum = TextBox1.Text;
double dblnum;
try
{
dblnum = System.Convert.ToDouble(strnum);
decimal xiaoshu = Math.Round((decimal)(dblnum - Math.Floor(dblnum)), 3);
Label2.Text = "";
Int64 num = System.Convert.ToInt64(Math.Floor(dblnum));
//过大判断
if ((num / 100000000000) >= 10) Label1.Text = "过大";
else
{
//为零判断
if (dblnum == 0) Label1.Text = "自己算去!";
else
{
if (num > 0) Label1.Text = Num2Str(num) + "<font color=red>圆</font>" +
xiaoshu2string(xiaoshu,num);
if (num == 0) Label1.Text = xiaoshu2string(xiaoshu,num);
}
}
//else Label1.Text = Num2Str(num);
//Response.Write(num.ToString());
}
catch
{
Label1.Text = "输入错误!";
}
}
public string Num2Str(Int64 num)
{
System.Text.StringBuilder result = new System.Text.StringBuilder();
//分解3个部分
long yi;
long wan;
long ge;
yi = System.Convert.ToInt64(Math.Floor((double) num /100000000));
wan =System.Convert.ToInt64( Math.Floor((double)(num - yi * 100000000) / (double)
10000));
ge= num - yi * 100000000 - wan * 10000;
//每个部分4位,调用函数,分别转大写
if (yi > 0) result.Append(FourBit2String(yi) + "<font color=red>亿</font>");
if (wan > 0) result.Append(FourBit2String(wan) + "<font color=red>万</font>");
if (yi>0 & wan==0) result.Append("零");
if (ge > 0) result.Append(FourBit2String(ge));
return result.ToString();
}
public string SingleNumber2Big(long n)
{
//单个数字转成大写
string big="";
switch(n)
{
case 9: big = "玖"; break;
case 8: big = "捌"; break;
case 7: big = "柒"; break;
case 6: big = "陆"; break;
case 5: big = "伍"; break;
case 4: big = "肆"; break;
case 3: big = "叁"; break;
case 2: big = "贰"; break;
case 1: big = "壹"; break;
case 0: big = "零"; break;
}
return big;
}
public string FourBit2String(long n)
{
//四位四位的转大写
long qian;
long bai;
long shi;
long ge;
qian =System.Convert.ToInt64( Math.Floor((double)n / (double)1000));
bai =System.Convert.ToInt64( Math.Floor((n-qian*1000)/(double)100));
shi =System.Convert.ToInt64( Math.Floor((n-qian*1000-bai*100)/(double)10));
ge = n - qian * 1000 - bai * 100 - shi * 10;
System.Text.StringBuilder four = new System.Text.StringBuilder();
if (qian > 0) four.Append(SingleNumber2Big(qian) + "仟");
if (bai > 0) four.Append(SingleNumber2Big(bai) + "佰");
if (qian > 0 & (bai == 0)&((ge>0)|(shi>0)) ) four.Append("零");
if (shi > 0) four.Append(SingleNumber2Big(shi) + "拾");
if (qian > 0 & (bai> 0) & (shi==0) & (ge>0 )) four.Append("零");
if (ge > 0) four.Append(SingleNumber2Big(ge));
return four.ToString();
}
public string xiaoshu2string(decimal xs,long num)
{
if (xs == 0) return "整";
//取小数后三位转换
System.Text.StringBuilder jfl = new System.Text.StringBuilder();
long jiao =System.Convert.ToInt64( Math.Floor(xs*10));
long fen = System.Convert.ToInt64( Math.Floor(xs * 100 - jiao * 10));
long li = System.Convert.ToInt64( Math.Floor(xs * 1000 - jiao * 100 - fen * 10));
if (jiao > 0) jfl.Append(SingleNumber2Big(jiao) + "角");
if (num>0 & jiao==0 & (fen>0 | li>0)) jfl.Append("零");
if (fen > 0) jfl.Append(SingleNumber2Big(fen) + "分");
if (li > 0) jfl.Append(SingleNumber2Big(li) + "厘");
return jfl.ToString();
}
}