其实很简单,根本用不着写得很长很长。每4位分开,数字后面加上十百千万等,另外再处理掉多余的零就可以了。
- static string GetChineseString(double number)
- {
- string[] cStr = new string[] { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "", "十", "百", "千" };
- string[] unitStr = new string[] { "", "万", "亿", "万", "兆"};
- long intPart = (long)number;
- string result = string.Empty;
- for (int i = 0; i < intPart.ToString().Length; i++)
- {
- int temp = (int)((long)(intPart / (long)Math.Pow(10, i)) % 10);
- if (i % 4 == 0) result = unitStr[(int)i / 4] + result;
- result = cStr[temp] + cStr[10 + i % 4] + result;
- }
- result = Regex.Replace(result, "(零[十百千])+", "零");
- result = Regex.Replace(result, "零{2,}", "零");
- result = Regex.Replace(result, "零([万亿兆])", "$1");
- if(result.Length>1)result = result.TrimEnd('零');
- if (number - intPart == 0) return result;
- string decimalPart = number.ToString().Split('.')[1];
- result += "点";
- for (int j = 0; j < decimalPart.Length; j++)
- {
- int temp = int.Parse(decimalPart[j].ToString());
- result += cStr[temp];
- }
- return result;
- }
- static string GetChineseString(long number)
- {
- string[] cStr =new string[] {"零","一","二","三","四","五","六","七","八","九","","十","百","千"};
- string[] unitStr = new string[] { "", "万", "亿", "万", "兆" };
- string result = string.Empty;
- for (int i = 0; i < number.ToString().Length; i++)
- {
- int temp = (int)((long)(number / (long)Math.Pow(10, i)) % 10);//获取第i位的数字
- if (i % 4 == 0) result = unitStr[(int)i / 4] + result;//检查是否需要加上万或亿等
- result = cStr[temp] + cStr[10 + i % 4] + result;
- }
- result = Regex.Replace(result, "(零[十百千])+", "零");
- result = Regex.Replace(result, "零{2,}", "零");
- result = Regex.Replace(result, "零([万亿兆])", "$1");
- if(result.Length>1)result = result.TrimEnd('零');
- return result;
- }