马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

阿拉伯数字转换为中文数字/中文数字转换为阿拉伯数字

[复制链接]
查看2190 | 回复0 | 2019-8-13 17:52:41 | 显示全部楼层 |阅读模式

项目中经常会格式化数据,转换数字的使用环境比较多,记录一下数字转换的方法!

如果需要转换为繁体中文,将数组里的汉字换成繁体中文即可。

1.阿拉伯数字转换为中文数字

  1. 1 /// <summary>
  2. 2 /// 阿拉伯数字转换成中文数字
  3. 3 /// </summary>
  4. 4 /// <param name="x"></param>
  5. 5 /// <returns></returns>
  6. 6 public string NumToChinese(string x)
  7. 7 {
  8. 8 string[] pArrayNum = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
  9. 9 //为数字位数建立一个位数组
  10. 10 string[] pArrayDigit = { "", "十", "百", "千" };
  11. 11 //为数字单位建立一个单位数组
  12. 12 string[] pArrayUnits = { "", "万", "亿", "万亿" };
  13. 13 var pStrReturnValue = ""; //返回值
  14. 14 var finger = 0; //字符位置指针
  15. 15 var pIntM = x.Length % 4; //取模
  16. 16 int pIntK;
  17. 17 if (pIntM > 0)
  18. 18 pIntK = x.Length / 4 + 1;
  19. 19 else
  20. 20 pIntK = x.Length / 4;
  21. 21 //外层循环,四位一组,每组最后加上单位: ",万亿,",",亿,",",万,"
  22. 22 for (var i = pIntK; i > 0; i--)
  23. 23 {
  24. 24 var pIntL = 4;
  25. 25 if (i == pIntK && pIntM != 0)
  26. 26 pIntL = pIntM;
  27. 27 //得到一组四位数
  28. 28 var four = x.Substring(finger, pIntL);
  29. 29 var P_int_l = four.Length;
  30. 30 //内层循环在该组中的每一位数上循环
  31. 31 for (int j = 0; j < P_int_l; j++)
  32. 32 {
  33. 33 //处理组中的每一位数加上地点的位
  34. 34 int n = Convert.ToInt32(four.Substring(j, 1));
  35. 35 if (n == 0)
  36. 36 {
  37. 37 if (j < P_int_l - 1 && Convert.ToInt32(four.Substring(j + 1, 1)) > 0 && !pStrReturnValue.EndsWith(pArrayNum[n]))
  38. 38 pStrReturnValue += pArrayNum[n];
  39. 39 }
  40. 40 else
  41. 41 {
  42. 42 if (!(n == 1 && (pStrReturnValue.EndsWith(pArrayNum[0]) | pStrReturnValue.Length == 0) && j == P_int_l - 2))
  43. 43 pStrReturnValue += pArrayNum[n];
  44. 44 pStrReturnValue += pArrayDigit[P_int_l - j - 1];
  45. 45 }
  46. 46 }
  47. 47 finger += pIntL;
  48. 48 //每组最后加上一个单位:",万,",",亿," 等
  49. 49 if (i < pIntK) //如果不是最高位的一组
  50. 50 {
  51. 51 if (Convert.ToInt32(four) != 0)
  52. 52 //如果所有4位不全是0则加上单位",万,",",亿,"等
  53. 53 pStrReturnValue += pArrayUnits[i - 1];
  54. 54 }
  55. 55 else
  56. 56 {
  57. 57 //处理最高位的一组,最后必须加上单位
  58. 58 pStrReturnValue += pArrayUnits[i - 1];
  59. 59 }
  60. 60 }
  61. 61 return pStrReturnValue;
  62. 62 }
复制代码

2.中文数字转换为阿拉伯数字

  1. 1 /// <summary>
  2. 2 /// 转换数字
  3. 3 /// </summary>
  4. 4 protected static long CharToNumber(char c)
  5. 5 {
  6. 6 switch (c)
  7. 7 {
  8. 8 case '一': return 1;
  9. 9 case '二': return 2;
  10. 10 case '三': return 3;
  11. 11 case '四': return 4;
  12. 12 case '五': return 5;
  13. 13 case '六': return 6;
  14. 14 case '七': return 7;
  15. 15 case '八': return 8;
  16. 16 case '九': return 9;
  17. 17 case '零': return 0;
  18. 18 default: return -1;
  19. 19 }
  20. 20 }
  21. 21
  22. 22 /// <summary>
  23. 23 /// 转换单位
  24. 24 /// </summary>
  25. 25 protected static long CharToUnit(char c)
  26. 26 {
  27. 27 switch (c)
  28. 28 {
  29. 29 case '十': return 10;
  30. 30 case '百': return 100;
  31. 31 case '千': return 1000;
  32. 32 case '万': return 10000;
  33. 33 case '亿': return 100000000;
  34. 34 default: return 1;
  35. 35 }
  36. 36 }
  37. 37 /// <summary>
  38. 38 /// 将中文数字转换阿拉伯数字
  39. 39 /// </summary>
  40. 40 /// <param name="cnum">汉字数字</param>
  41. 41 /// <returns>长整型阿拉伯数字</returns>
  42. 42 public static long ParseCnToInt(string cnum)
  43. 43 {
  44. 44 cnum = Regex.Replace(cnum, "\\s+", "");
  45. 45 long firstUnit = 1;//一级单位
  46. 46 long secondUnit = 1;//二级单位
  47. 47 long result = 0;//结果
  48. 48 for (var i = cnum.Length - 1; i > -1; --i)//从低到高位依次处理
  49. 49 {
  50. 50 var tmpUnit = CharToUnit(cnum[i]);//临时单位变量
  51. 51 if (tmpUnit > firstUnit)//判断此位是数字还是单位
  52. 52 {
  53. 53 firstUnit = tmpUnit;//是的话就赋值,以备下次循环使用
  54. 54 secondUnit = 1;
  55. 55 if (i == 0)//处理如果是"十","十一"这样的开头的
  56. 56 {
  57. 57 result += firstUnit * secondUnit;
  58. 58 }
  59. 59 continue;//结束本次循环
  60. 60 }
  61. 61 if (tmpUnit > secondUnit)
  62. 62 {
  63. 63 secondUnit = tmpUnit;
  64. 64 continue;
  65. 65 }
  66. 66 result += firstUnit * secondUnit * CharToNumber(cnum[i]);//如果是数字,则和单位想乘然后存到结果里
  67. 67 }
  68. 68 return result;
  69. 69 }
复制代码


来源:https://www.cnblogs.com/yellow3gold/p/11338374.html
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则