标题: 求教关于C#中数字转换为大写汉字的技术 [打印本页] 作者: ARMdong 时间: 2012-7-20 22:44 标题: 求教关于C#中数字转换为大写汉字的技术 今天做黑马的基础测试题的时候,遇到一题。说是给定一个时间,如:2012年07月20日,然后让你将其转换为大写汉字表示,如:二零一二年七月二十日。
在做这道题目的时候我只想到了用SubString()这个方法还有switch-case来解决,但是具体怎么实现,我还是没有想出来,求解!希望大家帮帮忙,教教小弟怎么解决这道题。感谢……作者: 刘建素 时间: 2012-7-20 23:33
#region 输出事件
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtDate.Text.Trim()))
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "test", @"alert('输入不为空');", true);
}
try
{
string result = "";
DateTime time = Convert.ToDateTime(txtDate.Text);
string year = time.Year.ToString();
int month = time.Month;
int day = time.Day;
result += GetStr(year[0].ToString()) + GetStr(year[1].ToString()) + GetStr(year[2].ToString()) + GetStr(year[3].ToString()) + "年";
if (month <= 9)
{
result += GetStr(month.ToString()) + "月";
}
else
{
switch (month)
{
case 10:
result += "十月";
break;
case 11:
result += "十一月";
break;
case 12:
result += "十二月";
break;
}
}
if (day <= 9)
{
result += GetStr(day.ToString()) + "日";
}
else if (day == 10)
{
result += "十日";
}
else if (10 < day && day < 20)
{
result += "十" + GetStr(GetStr(day.ToString()[1].ToString()))+"日";
}
else if(day==20)
{
result += "二十日";
}else if(20<day&&day<30)
{
result += "二十" + GetStr(GetStr(day.ToString()[1].ToString())) + "日";
}
else if (day == 30)
{
result += "三十日";
}
else
{
result += "三十一日";
}
//litMsg.Text = time.Year + "年" + time.Month + "月" + time.Day + "日";
//litMsg.Visible = true;
}
catch
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "test", @"alert('时间格式错误');", true);
}
}
#endregion
public string GetStr(string str)
{
string result = "";
switch (str)
{
case "0:":
result = "零";
break;
case "1":
result = "一";
break;
case "2":
result = "二";
break;
case "3":
result = "三";
break;
case "4":
result = "四";
break;
case "5":
result = "五";
break;
case "6":
result = "六";
break;
case "7":
result = "七";
break;
case "8":
result = "八";
break;
case "9":
result = "九";
break;
}
return result;
}作者: 高宜 时间: 2012-7-21 00:17 本帖最后由 高宜 于 2012-7-21 00:18 编辑