| 
 项目中经常会格式化数据,转换数字的使用环境比较多,记录一下数字转换的方法! 
如果需要转换为繁体中文,将数组里的汉字换成繁体中文即可。 
1.阿拉伯数字转换为中文数字 
-  1 /// <summary>
 -  2 /// 阿拉伯数字转换成中文数字
 -  3 /// </summary>
 -  4 /// <param name="x"></param>
 -  5 /// <returns></returns>
 -  6 public string NumToChinese(string x)
 -  7 {
 -  8     string[] pArrayNum = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
 -  9     //为数字位数建立一个位数组  
 - 10     string[] pArrayDigit = { "", "十", "百", "千" };
 - 11     //为数字单位建立一个单位数组  
 - 12     string[] pArrayUnits = { "", "万", "亿", "万亿" };
 - 13     var pStrReturnValue = ""; //返回值  
 - 14     var finger = 0; //字符位置指针  
 - 15     var pIntM = x.Length % 4; //取模  
 - 16     int pIntK;
 - 17     if (pIntM > 0)
 - 18         pIntK = x.Length / 4 + 1;
 - 19     else
 - 20         pIntK = x.Length / 4;
 - 21     //外层循环,四位一组,每组最后加上单位: ",万亿,",",亿,",",万,"  
 - 22     for (var i = pIntK; i > 0; i--)
 - 23     {
 - 24         var pIntL = 4;
 - 25         if (i == pIntK && pIntM != 0)
 - 26             pIntL = pIntM;
 - 27         //得到一组四位数  
 - 28         var four = x.Substring(finger, pIntL);
 - 29         var P_int_l = four.Length;
 - 30         //内层循环在该组中的每一位数上循环  
 - 31         for (int j = 0; j < P_int_l; j++)
 - 32         {
 - 33             //处理组中的每一位数加上地点的位  
 - 34             int n = Convert.ToInt32(four.Substring(j, 1));
 - 35             if (n == 0)
 - 36             {
 - 37                 if (j < P_int_l - 1 && Convert.ToInt32(four.Substring(j + 1, 1)) > 0 && !pStrReturnValue.EndsWith(pArrayNum[n]))
 - 38                     pStrReturnValue += pArrayNum[n];
 - 39             }
 - 40             else
 - 41             {
 - 42                 if (!(n == 1 && (pStrReturnValue.EndsWith(pArrayNum[0]) | pStrReturnValue.Length == 0) && j == P_int_l - 2))
 - 43                     pStrReturnValue += pArrayNum[n];
 - 44                 pStrReturnValue += pArrayDigit[P_int_l - j - 1];
 - 45             }
 - 46         }
 - 47         finger += pIntL;
 - 48         //每组最后加上一个单位:",万,",",亿," 等  
 - 49         if (i < pIntK) //如果不是最高位的一组  
 - 50         {
 - 51             if (Convert.ToInt32(four) != 0)
 - 52                 //如果所有4位不全是0则加上单位",万,",",亿,"等  
 - 53                 pStrReturnValue += pArrayUnits[i - 1];
 - 54         }
 - 55         else
 - 56         {
 - 57             //处理最高位的一组,最后必须加上单位  
 - 58             pStrReturnValue += pArrayUnits[i - 1];
 - 59         }
 - 60     }
 - 61     return pStrReturnValue;
 - 62 }
 
  复制代码 
  
2.中文数字转换为阿拉伯数字 
  
-  1 /// <summary>
 -  2 /// 转换数字
 -  3 /// </summary>
 -  4 protected static long CharToNumber(char c)
 -  5 {
 -  6     switch (c)
 -  7     {
 -  8         case '一': return 1;
 -  9         case '二': return 2;
 - 10         case '三': return 3;
 - 11         case '四': return 4;
 - 12         case '五': return 5;
 - 13         case '六': return 6;
 - 14         case '七': return 7;
 - 15         case '八': return 8;
 - 16         case '九': return 9;
 - 17         case '零': return 0;
 - 18         default: return -1;
 - 19     }
 - 20 }
 - 21 
 - 22 /// <summary>
 - 23 /// 转换单位
 - 24 /// </summary>
 - 25 protected static long CharToUnit(char c)
 - 26 {
 - 27     switch (c)
 - 28     {
 - 29         case '十': return 10;
 - 30         case '百': return 100;
 - 31         case '千': return 1000;
 - 32         case '万': return 10000;
 - 33         case '亿': return 100000000;
 - 34         default: return 1;
 - 35     }
 - 36 }
 - 37 /// <summary>
 - 38 /// 将中文数字转换阿拉伯数字
 - 39 /// </summary>
 - 40 /// <param name="cnum">汉字数字</param>
 - 41 /// <returns>长整型阿拉伯数字</returns>
 - 42 public static long ParseCnToInt(string cnum)
 - 43 {
 - 44     cnum = Regex.Replace(cnum, "\\s+", "");
 - 45     long firstUnit = 1;//一级单位                
 - 46     long secondUnit = 1;//二级单位 
 - 47     long result = 0;//结果
 - 48     for (var i = cnum.Length - 1; i > -1; --i)//从低到高位依次处理
 - 49     {
 - 50         var tmpUnit = CharToUnit(cnum[i]);//临时单位变量
 - 51         if (tmpUnit > firstUnit)//判断此位是数字还是单位
 - 52         {
 - 53             firstUnit = tmpUnit;//是的话就赋值,以备下次循环使用
 - 54             secondUnit = 1;
 - 55             if (i == 0)//处理如果是"十","十一"这样的开头的
 - 56             {
 - 57                 result += firstUnit * secondUnit;
 - 58             }
 - 59             continue;//结束本次循环
 - 60         }
 - 61         if (tmpUnit > secondUnit)
 - 62         {
 - 63             secondUnit = tmpUnit;
 - 64             continue;
 - 65         }
 - 66         result += firstUnit * secondUnit * CharToNumber(cnum[i]);//如果是数字,则和单位想乘然后存到结果里
 - 67     }
 - 68     return result;
 - 69 }
 
  复制代码 
   来源:https://www.cnblogs.com/yellow3gold/p/11338374.html 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |