第一种方法是一种笨方法,感觉代码太多,也很不方便,所以又重新写了这一个,应该是比较全面了。
经Tony.Gong提醒,修改后的代码如下:
public string GetUpperNum(string numStr)
{
if(numStr==null || numStr=="") return "没有数值!";
string[] n=new string[10]{"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
string[] u=new string[11]{"角","分","元","拾","佰","仟","万","拾","佰","仟","亿"};
string result="";
numStr=decimal.Round(decimal.Parse(numStr),2).ToString();
string[] num=numStr.Split(new char[]{'.'});
if(num[0].Length>9) return "超出范围";
//整数部分
for(int i=0;i<num[0].Length;i++)
{
switch(num[0].Substring(i,1))
{
case "0":
if(!(num[0].Substring(i-1,1)=="0")) result=result+n[0];
break;
case "1":
result=result+n[1]+u[num[0].Length-i+1];
break;
case "2":
result=result+n[2]+u[num[0].Length-i+1];
break;
case "3":
result=result+n[3]+u[num[0].Length-i+1];
break;
case "4":
result=result+n[4]+u[num[0].Length-i+1];
break;
case "5":
result=result+n[5]+u[num[0].Length-i+1];
break;
case "6":
result=result+n[6]+u[num[0].Length-i+1];
break;
case "7":
result=result+n[7]+u[num[0].Length-i+1];
break;
case "8":
result=result+n[8]+u[num[0].Length-i+1];
break;
case "9":
result=result+n[9]+u[num[0].Length-i+1];
break;
}
}
//处理万位以上零数
switch(num[0].Length)
{
case 6:
if(num[0].Substring(1,1)=="0") result=result.Substring(0,2)+u[6]+result.Substring(2,result.Length-2);
break;
case 7:
if(num[0].Substring(1,2)=="00")
{
result=result.Substring(0,2)+u[6]+result.Substring(2,result.Length-2);
break;
}
if(num[0].Substring(2,1)=="0") result=result.Substring(0,4)+u[6]+result.Substring(4,result.Length-4);
break;
case 8:
if(num[0].Substring(1,3)=="000")
{
result=result.Substring(0,2)+u[6]+result.Substring(2,result.Length-2);
break;
}
if(num[0].Substring(1,1)=="0" && (!(num[0].Substring(2,1)=="0")) && num[0].Substring(3,1)=="0")
{
result=result.Substring(0,5)+u[6]+result.Substring(5,result.Length-5);
break;
}
if(num[0].Substring(2,2)=="00")
{
result=result.Substring(0,4)+u[6]+result.Substring(4,result.Length-4);
break;
}
if(num[0].Substring(3,1)=="0") result=result.Substring(0,6)+u[6]+result.Substring(6,result.Length-6);
break;
case 9:
{
if(num[0].Substring(1,4)=="0000") break;
if(num[0].Substring(1,1)=="0" && (!(num[0].Substring(2,1)=="0")) && num[0].Substring(3,1)=="0" && num[0].Substring(4,1)=="0")
{
result=result.Substring(0,5)+u[6]+result.Substring(5,result.Length-5);
break;
}
if(num[0].Substring(1,1)=="0" && (!(num[0].Substring(3,1)=="")) && num[0].Substring(2,1)=="0" && num[0].Substring(4,1)=="0")
{
result=result.Substring(0,5)+u[6]+result.Substring(5,result.Length-5);
break;
}
if(num[0].Substring(1,1)=="0" && (!(num[0].Substring(2,1)=="0")) && (!(num[0].Substring(3,1)=="0")) && num[0].Substring(4,1)=="0")
{
result=result.Substring(0,7)+u[6]+result.Substring(7,result.Length-7);
break;
}
if((!(num[0].Substring(1,1)=="0")) && num[0].Substring(2,1)=="0" && (!(num[0].Substring(3,1)=="0")) && num[0].Substring(4,1)=="0")
{
result=result.Substring(0,7)+u[6]+result.Substring(7,result.Length-7);
break;
}
if(num[0].Substring(2,3)=="000")
{
result=result.Substring(0,4)+u[6]+result.Substring(4,result.Length-4);
break;
}
if(num[0].Substring(3,2)=="00")
{
result=result.Substring(0,6)+u[6]+result.Substring(6,result.Length-6);
break;
}
if(num[0].Substring(4,1)=="0") result=result.Substring(0,8)+u[6]+result.Substring(8,result.Length-8);
break;
}
}
//处理整数的尾数零
if(result.Substring(result.Length-1,1)=="零") result=result.Substring(0,result.Length-1)+"元";
//小数部分
if(num.Length<2) result=result+"整";
else
{
if(num[1]=="00") return result+"整";
if(num[1].Length==1 && num[1].Substring(0,1)=="0") return result+"整";
for(int i=0;i<num[1].Length;i++)
{
switch(num[1].Substring(i,1))
{
case "0":
if(i==0) result=result+n[0];
break;
case "1":
result=result+n[1]+u[i];
break;
case "2":
result=result+n[2]+u[i];
break;
case "3":
result=result+n[3]+u[i];
break;
case "4":
result=result+n[4]+u[i];
break;
case "5":
result=result+n[5]+u[i];
break;
case "6":
result=result+n[6]+u[i];
break;
case "7":
result=result+n[7]+u[i];
break;
case "8":
result=result+n[8]+u[i];
break;
case "9":
result=result+n[9]+u[i];
break;
}
}
}
return result ;
}