目前只支持”万“以下的转换。
private static string GetNumber4Chinese(string str)
{
string istr = "0";
string cstr = string.Empty;
string nstr = string.Empty;
for (int i = 0; i < str.Length; i++)
{
cstr = str.Substring(i, 1);
nstr = (i < str.Length - 1) ? str.Substring(i + 1, 1) : "个";
switch (cstr)
{
case "一":
case "壹":
istr = (Convert.ToInt64(istr) + 1 * GetBaseNumber4ChineseUnit(nstr)).ToString();
break;
case "二":
case "两":
case "贰":
istr = (Convert.ToInt64(istr) + 2 * GetBaseNumber4ChineseUnit(nstr)).ToString();
break;
case "三":
case "叁":
istr = (Convert.ToInt64(istr) + 3 * GetBaseNumber4ChineseUnit(nstr)).ToString();
break;
case "四":
case "肆":
istr = (Convert.ToInt64(istr) + 4 * GetBaseNumber4ChineseUnit(nstr)).ToString();
break;
case "五":
case "伍":
istr = (Convert.ToInt64(istr) + 5 * GetBaseNumber4ChineseUnit(nstr)).ToString();
break;
case "六":
case "陆":
istr = (Convert.ToInt64(istr) + 6 * GetBaseNumber4ChineseUnit(nstr)).ToString();
break;
case "七":
case "柒":
istr = (Convert.ToInt64(istr) + 7 * GetBaseNumber4ChineseUnit(nstr)).ToString();
break;
case "八":
case "捌":
istr = (Convert.ToInt64(istr) + 8 * GetBaseNumber4ChineseUnit(nstr)).ToString();
break;
case "九":
case "玖":
istr = (Convert.ToInt64(istr) + 9 * GetBaseNumber4ChineseUnit(nstr)).ToString();
break;
default:
if (str.Length < 2)
{
istr = GetBaseNumber4ChineseUnit(cstr).ToString();
}
else if (str.Length == 2 && (str.StartsWith("十") || str.StartsWith("拾")))
{
return (10 + Convert.ToInt64(GetNumber4Chinese(nstr))).ToString();
}
break;
}
}
return istr;
}
private static int GetBaseNumber4ChineseUnit(string str)
{
int ibase = 0;
string cstr = "0";
for (int i = 0; i < str.Length; i++)
{
cstr = str.Substring(i, 1);
switch (cstr)
{
case "万":
ibase = 10000;
break;
case "千":
ibase = 1000;
break;
case "百":
ibase = 100;
break;
case "十":
ibase = 10;
break;
case "个":
ibase = 1;
break;
}
}
return ibase;
}