数字转为大写(C#)
/// <summary>
/// 数字转为中文()
/// </summary>
/// <param name="strNum">数字字符串</param>
/// <returns>转化后的中文字符串</returns>
public static string NumToCN(string strNum)
{
//数字转换为中文后的数组
string[] CN_num = new string[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
//为数字位数建立一个位数组
string[] CN_digit = new string[] { "", "拾", "佰", "仟" };
//为数字单位建立一个单位数组
string[] CN_units = new string[] { "", "万", "亿", "万" };
string str_returnValue = ""; //返回值
int finger = 0; //字符位置指针
int field_Length; //字段长度
int field_Num; //字段数量
if (strNum.Length % 4 == 0)
{
field_Num = strNum.Length / 4;
field_Length = 4;
}
else
{
field_Num = strNum.Length / 4 + 1;
field_Length = strNum.Length % 4;
}
for (int i = 0; i < field_Num; i++)
{
string four = strNum.Substring(finger, field_Length);
finger += field_Length;
for (int j = 0; j < four.Length; j++)
{
if (four[j] != '0')
{
str_returnValue = str_returnValue + CN_num[Convert.ToInt32(four[j].ToString())];
str_returnValue = str_returnValue + CN_digit[field_Length - 1];
}
else
{
if (j != four.Length - 1 && four[j + 1] != four[j])
{
str_returnValue = str_returnValue + CN_num[Convert.ToInt32(four[j].ToString())];
}
}
if (j != four.Length - 1)
{
field_Length--;
}
else
{
if (Convert.ToInt32(four) != 0 || field_Num - 1 - i ==2)
{
str_returnValue = str_returnValue + CN_units[field_Num - 1 - i];
}
field_Length = 4;
}
}
}
return str_returnValue + "元";
}