C# 数字转换成汉字大写 数值转换成汉字大写

1.数字转换成汉字大写

public string NumToChinese(string x)
{
//数字转换为中文后的数组 //转载请注明来自 http://www.shang11.com
string[] P_array_num = new string[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
//为数字位数建立一个位数组
string[] P_array_digit = new string[] { "", "拾", "佰", "仟" };
//为数字单位建立一个单位数组
string[] P_array_units = new string[] { "", "万", "亿", "万亿" };
string P_str_returnValue = ""; //返回值
int finger = ; //字符位置指针
int P_int_m = x.Length % ; //取模
int P_int_k = ;
if (P_int_m > )
P_int_k = x.Length / + ;
else
P_int_k = x.Length / ;
//外层循环,四位一组,每组最后加上单位: ",万亿,",",亿,",",万,"
for (int i = P_int_k; i > ; i--)
{
int P_int_L = ;
if (i == P_int_k && P_int_m != )
P_int_L = P_int_m;
//得到一组四位数
string four = x.Substring(finger, P_int_L);
int P_int_l = four.Length;
//内层循环在该组中的每一位数上循环
for (int j = ; j < P_int_l; j++)
{
//处理组中的每一位数加上所在的位
int n = Convert.ToInt32(four.Substring(j, ));
if (n == )
{
if (j < P_int_l - && Convert.ToInt32(four.Substring(j + , )) > && !P_str_returnValue.EndsWith(P_array_num[n]))
P_str_returnValue += P_array_num[n];
}
else
{
if (!(n == && (P_str_returnValue.EndsWith(P_array_num[]) | P_str_returnValue.Length == ) && j == P_int_l - ))
P_str_returnValue += P_array_num[n];
P_str_returnValue += P_array_digit[P_int_l - j - ];
}
}
finger += P_int_L;
//每组最后加上一个单位:",万,",",亿," 等
if (i < P_int_k) //如果不是最高位的一组
{
if (Convert.ToInt32(four) != )
//如果所有4位不全是0则加上单位",万,",",亿,"等
P_str_returnValue += P_array_units[i - ];
}
else
{
//处理最高位的一组,最后必须加上单位
P_str_returnValue += P_array_units[i - ];
}
}
return P_str_returnValue;
}

2.数值转换成汉字大写

   public static string GetChinaMoney(decimal money)
{
string[] strArray;
string str = "";
string str2 = "";
string str3 = money.ToString("0.00");
switch (str3.Trim().Length)
{
case :
strArray = new string[] { str3[].ToString(), "y", str3[].ToString(), "j", str3[].ToString(), "f" };
str = string.Concat(strArray);
break; case :
strArray = new string[] { str3[].ToString(), "s", str3[].ToString(), "y", str3[].ToString(), "j", str3[].ToString(), "f" };
str = string.Concat(strArray);
break; case :
strArray = new string[] { str3[].ToString(), "b", str3[].ToString(), "s", str3[].ToString(), "y", str3[].ToString(), "j", str3[].ToString(), "f" };
str = string.Concat(strArray);
break; case :
strArray = new string[] { str3[].ToString(), "q", str3[].ToString(), "b", str3[].ToString(), "s", str3[].ToString(), "y", str3[].ToString(), "j", str3[].ToString(), "f" };
str = string.Concat(strArray);
break; case :
strArray = new string[] { str3[].ToString(), "w", str3[].ToString(), "q", str3[].ToString(), "b", str3[].ToString(), "s", str3[].ToString(), "y", str3[].ToString(), "j", str3[].ToString(), "f" };
str = string.Concat(strArray);
break; case :
strArray = new string[] { str3[].ToString(), "s", str3[].ToString(), "w", str3[].ToString(), "q", str3[].ToString(), "b", str3[].ToString(), "s", str3[].ToString(), "y", str3[].ToString(), "j", str3[].ToString(), "f" };
str = string.Concat(strArray);
break; case :
strArray = new string[] {
str3[].ToString(), "b", str3[].ToString(), "s", str3[].ToString(), "w", str3[].ToString(), "q", str3[].ToString(), "b", str3[].ToString(), "s", str3[].ToString(), "y", str3[].ToString(), "j",
str3[].ToString(), "f"
};
str = string.Concat(strArray);
break; case :
strArray = new string[] {
str3[].ToString(), "q", str3[].ToString(), "b", str3[].ToString(), "s", str3[].ToString(), "w", str3[].ToString(), "q", str3[].ToString(), "b", str3[].ToString(), "s", str3[].ToString(), "y",
str3[].ToString(), "j", str3[].ToString(), "f"
};
str = string.Concat(strArray);
break; case :
strArray = new string[] {
str3[].ToString(), "m", str3[].ToString(), "q", str3[].ToString(), "b", str3[].ToString(), "s", str3[].ToString(), "w", str3[].ToString(), "q", str3[].ToString(), "b", str3[].ToString(), "s",
str3[].ToString(), "y", str3[].ToString(), "j", str3[].ToString(), "f"
};
str = string.Concat(strArray);
break;
}
for (int i = ; i < str.Trim().Length; i++)
{
switch (str[i])
{
case '':
str2 = str2 + "零";
break; case '':
str2 = str2 + "壹";
break; case '':
str2 = str2 + "贰";
break; case '':
str2 = str2 + "叁";
break; case '':
str2 = str2 + "肆";
break; case '':
str2 = str2 + "伍";
break; case '':
str2 = str2 + "陆";
break; case '':
str2 = str2 + "柒";
break; case '':
str2 = str2 + "捌";
break; case '':
str2 = str2 + "玖";
break; case 'b':
str2 = str2 + "佰";
break; case 'f':
str2 = str2 + "分";
break; case 'j':
str2 = str2 + "角";
break; case 'm':
str2 = str2 + "亿";
break; case 'q':
str2 = str2 + "仟";
break; case 's':
str2 = str2 + "拾";
break; case 'w':
str2 = str2 + "万";
break; case 'y':
str2 = str2 + "元";
break;
}
}
return str2;
}
上一篇:Java设计模式----中介者模式


下一篇:c头文件包含关系--记今天调试的郁闷经历