将数字转化为中文

其实很简单,根本用不着写得很长很长。每4位分开,数字后面加上十百千万等,另外再处理掉多余的零就可以了。

 


  1. static string GetChineseString(double number)  
  2. {  
  3.     string[] cStr = new string[] { "零""一""二""三""四""五""六""七""八""九""""十""百""千" };  
  4.     string[] unitStr = new string[] { """万""亿""万""兆"};  
  5.     long intPart = (long)number;  
  6.     string result = string.Empty;  
  7.     for (int i = 0; i < intPart.ToString().Length; i++)  
  8.     {  
  9.         int temp = (int)((long)(intPart / (long)Math.Pow(10, i)) % 10);  
  10.         if (i % 4 == 0) result = unitStr[(int)i / 4] + result;  
  11.         result = cStr[temp] + cStr[10 + i % 4] + result;  
  12.     }  
  13.     result = Regex.Replace(result, "(零[十百千])+""零");  
  14.     result = Regex.Replace(result, "零{2,}""零");  
  15.     result = Regex.Replace(result, "零([万亿兆])""$1");  
  16.     if(result.Length>1)result = result.TrimEnd('零');  
  17.     if (number - intPart == 0) return result;  
  18.     string decimalPart = number.ToString().Split('.')[1];  
  19.     result += "点";  
  20.     for (int j = 0; j < decimalPart.Length; j++)  
  21.     {  
  22.         int temp = int.Parse(decimalPart[j].ToString());  
  23.         result += cStr[temp];  
  24.     }  
  25.     return result;  
  26. }  
  27.   
  28. static string GetChineseString(long number)  
  29. {  
  30.     string[] cStr =new string[] {"零","一","二","三","四","五","六","七","八","九","","十","百","千"};  
  31.     string[] unitStr = new string[] { """万""亿""万""兆" };  
  32.     string result = string.Empty;  
  33.     for (int i = 0; i < number.ToString().Length; i++)  
  34.     {  
  35.         int temp = (int)((long)(number / (long)Math.Pow(10, i)) % 10);//获取第i位的数字  
  36.         if (i % 4 == 0) result = unitStr[(int)i / 4] + result;//检查是否需要加上万或亿等  
  37.         result = cStr[temp] + cStr[10 + i % 4] + result;  
  38.     }  
  39.     result = Regex.Replace(result, "(零[十百千])+""零");  
  40.     result = Regex.Replace(result, "零{2,}""零");  
  41.     result = Regex.Replace(result, "零([万亿兆])""$1");  
  42.     if(result.Length>1)result = result.TrimEnd('零');  
  43.     return result;  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值