const string NUM = "零一二三四五六七八九";
const string NUM1 = " 十百千";
const string NUM2 = " 万亿";
public string speak(string subnum)
{
if (subnum.Trim('0') == "")
return "零";
string text = "";
for (int i = 0; i < subnum.Length; i++)
{
char c = subnum[i];
if (c > '0')
{
text += NUM[int.Parse(c.ToString())].ToString();
text += NUM1[subnum.Length - i - 1];
}
else
text += "零";
}
text = text.Trim(' ');
if (text.Length > 1)
text = text.TrimEnd('零');
return text;
}
public string ChinaSpeak(string num)
{
num = num.PadLeft(12, '0');
int n = num.Length / 4;
string text = "";
for (int i = 0; i < n; i++)
{
string t = speak(num.Substring(i * 4, 4));
text += t;
if (t != "零" && t.Length > 0)
text += NUM2[n - i - 1];
}
text = text.Trim('零').Trim(' ');
while (text.Contains("零零"))
text = text.Replace("零零", "零");
if (text.Length > 1 && text.Substring(0, 2) == "一十")
text = text.Remove(0, 1);
return text;
}
调用示例:
Console.WriteLine(ChinaSpeak("12"));
Console.WriteLine(ChinaSpeak("102"));
Console.WriteLine(ChinaSpeak("1002"));
Console.WriteLine(ChinaSpeak("112"));
Console.WriteLine(ChinaSpeak("10112"));
Console.WriteLine(ChinaSpeak("120000000"));
Console.WriteLine(ChinaSpeak("11021002"));
Console.WriteLine(ChinaSpeak("11020102"));
Console.WriteLine(ChinaSpeak("1000001"));
Console.WriteLine(ChinaSpeak("1000000001"));