人民币大小写转换

  1. using System;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4. namespace HKH.Common
  5. {
  6.     /// <summary>
  7.     /// 人民币大小写格式转换
  8.     /// </summary>
  9.     /// <remarks> 范中磊
  10.     /// </remarks>
  11.     public class clsRMB
  12.     {
  13.         private clsRMB()
  14.         {
  15.         }
  16.         #region 格式化
  17.         /// <summary>
  18.         /// 格式化(大写转小写)
  19.         /// </summary>
  20.         /// <param name="strRMB"></param>
  21.         /// <returns></returns>
  22.         public static double Format(string strRMB)
  23.         {
  24.             try
  25.             {
  26.                 //正则表达式,验证第一位是否阿拉伯数字,确定转换格式
  27.                 //1.5亿----混写格式
  28.                 if (Regex.IsMatch(strRMB, "^//d"))
  29.                 {
  30.                     //去掉元单位
  31.                     strRMB = Regex.Replace(strRMB, "元|圆""");
  32.                     char temp = strRMB[strRMB.Length - 1];
  33.                     if (temp == '万' || temp == '萬' || temp == '亿')
  34.                     {
  35.                         return Convert.ToDouble(strRMB.Substring(0, strRMB.Length - 1)) * Math.Pow(10, GetExp(temp));
  36.                     }
  37.                     else
  38.                     {
  39.                         return Convert.ToDouble(strRMB);
  40.                     }
  41.                 }
  42.                 //壹亿伍千万-----大写格式
  43.                 else
  44.                 {
  45.                     return Eval(strRMB);
  46.                 }
  47.             }
  48.             catch
  49.             {
  50.                 return -1;
  51.             }
  52.         }
  53.         /// <summary>
  54.         /// 格式化(小写转大写)
  55.         /// </summary>
  56.         /// <param name="numRMB"></param>
  57.         /// <returns></returns>
  58.         public static string Format(double numRMB)
  59.         {
  60.             try
  61.             {
  62.                 if (0 == numRMB)
  63.                     return "零元整";
  64.                 StringBuilder szRMB = new StringBuilder();
  65.                 //乘100以格式成整型,便于处理
  66.                 ulong iRMB = Convert.ToUInt64(numRMB * 100);
  67.                 szRMB.Insert(0, ToUpper(Convert.ToInt32(iRMB % 100), -2));
  68.                 //去掉原来的小数位
  69.                 iRMB = iRMB / 100;
  70.                 int iUnit = 0;
  71.                 //以每4位为一个单位段进行处理,所以下边除以10000
  72.                 while (iRMB != 0)
  73.                 {
  74.                     szRMB.Insert(0, ToUpper(Convert.ToInt32(iRMB % 10000), iUnit));
  75.                     iRMB = iRMB / 10000;
  76.                     iUnit += 4;
  77.                 }
  78.                 string strRMB = szRMB.ToString();
  79.                 //格式修正
  80.                 strRMB = Regex.Replace(strRMB, "零+""零");
  81.                 strRMB = strRMB.Replace("元零整""元整");
  82.                 strRMB = strRMB.Replace("零元""元");
  83.                 return strRMB.Trim('零');
  84.             }
  85.             catch
  86.             {
  87.                 return "";
  88.             }
  89.         }
  90.         #endregion
  91.         #region 私有方法
  92.         /// <summary>
  93.         /// 计算表达式(大写表达式求值)
  94.         /// </summary>
  95.         /// <param name="strRMB"></param>
  96.         /// <returns></returns>
  97.         private static double Eval(string strRMB)
  98.         {
  99.             try
  100.             {
  101.                 if (null == strRMB)
  102.                     return 0;
  103.                 strRMB = Replace(strRMB, false);
  104.                 if ("" == strRMB)
  105.                     return 0;
  106.                 #region 利用位权进行计算
  107.                 //基础指数
  108.                 int basicExp = 0;
  109.                 //当前指数
  110.                 int currExp = 0;
  111.                 double numRMB = 0;
  112.                 for (int i = strRMB.Length - 1; i > -1; i--)
  113.                 {
  114.                     char temp = strRMB[i];
  115.                     if (temp == '元' || temp == '万' || temp == '亿' || temp == '圆' || temp == '萬')
  116.                     {
  117.                         basicExp = GetExp(temp);
  118.                         currExp = 0;
  119.                         continue;
  120.                     }
  121.                     else
  122.                     {
  123.                         if (Regex.IsMatch(temp.ToString(), "^//d"))
  124.                         {
  125.                             numRMB = numRMB + Convert.ToInt32(temp.ToString()) * Math.Pow(10, (basicExp + currExp));
  126.                         }
  127.                         else
  128.                         {
  129.                             currExp = GetExp(temp);
  130.                         }
  131.                     }
  132.                 }
  133.                 #endregion
  134.                 return numRMB;
  135.             }
  136.             catch
  137.             {
  138.                 return -1;
  139.             }
  140.         }
  141.         /// <summary>
  142.         /// 计算表达式(小写数值求大写字符串)
  143.         /// </summary>
  144.         /// <param name="numRMB"></param>
  145.         /// <param name="iUnit"></param>
  146.         /// <returns></returns>
  147.         private static string ToUpper(int numRMB, int iUnit)
  148.         {
  149.             try
  150.             {
  151.                 if (0 == numRMB)
  152.                 {
  153.                     if (iUnit == -2)
  154.                     {
  155.                         return "整";
  156.                     }
  157.                     if (iUnit == 0)
  158.                     {
  159.                         return "元";
  160.                     }
  161.                     return "零";
  162.                 }
  163.                 StringBuilder szRMB = new StringBuilder();
  164.                 string strRMB = "";
  165.                 #region 对角/分做特殊处理
  166.                 if (iUnit == -2)
  167.                 {
  168.                     int jiao = numRMB / 10;
  169.                     int fen = numRMB % 10;
  170.                     if (jiao > 0)
  171.                     {
  172.                         szRMB.Append(jiao);
  173.                         szRMB.Append(GetUnit(-1));
  174.                         if (fen > 0)
  175.                         {
  176.                             szRMB.Append(fen);
  177.                             szRMB.Append(GetUnit(-2));
  178.                         }
  179.                     }
  180.                     else
  181.                     {
  182.                         szRMB.Append(fen);
  183.                         szRMB.Append(GetUnit(-2));
  184.                     }
  185.                     return Replace(szRMB.ToString(), true);
  186.                 }
  187.                 #endregion
  188.                 #region 以下为整数部分正常处理
  189.                 strRMB = numRMB.ToString("0000");
  190.                 //前一位是否是0
  191.                 bool hasZero = false;
  192.                 for (int i = 0; i < strRMB.Length; i++)
  193.                 {
  194.                     //只有四位,最高位为‘千’,所以下边的3-i为单位修正
  195.                     if ((3 - i) > 0)
  196.                     {
  197.                         if ('0' != strRMB[i])
  198.                         {
  199.                             szRMB.Append(strRMB[i]);
  200.                             szRMB.Append(GetUnit(3 - i));
  201.                             hasZero = false;
  202.                         }
  203.                         else
  204.                         {
  205.                             if (!hasZero)
  206.                                 szRMB.Append(strRMB[i]);
  207.                             hasZero = true;
  208.                         }
  209.                     }
  210.                     //最后一位,特别格式处理
  211.                     //如最后一位是零,则单位应在零之前
  212.                     else
  213.                     {
  214.                         if ('0' != strRMB[i])
  215.                         {
  216.                             szRMB.Append(strRMB[i]);
  217.                             szRMB.Append(GetUnit(iUnit));
  218.                             hasZero = false;
  219.                         }
  220.                         else
  221.                         {
  222.                             if (hasZero)
  223.                             {
  224.                                 szRMB.Insert(szRMB.Length - 1, GetUnit(iUnit));
  225.                             }
  226.                             else
  227.                             {
  228.                                 szRMB.Append(GetUnit(iUnit));
  229.                                 szRMB.Append(strRMB[i]);
  230.                             }
  231.                         }
  232.                     }
  233.                 }
  234.                 //转换大写后返回
  235.                 return Replace(szRMB.ToString(), true);
  236.                 #endregion
  237.             }
  238.             catch
  239.             {
  240.                 return "";
  241.             }
  242.         }
  243.         /// <summary>
  244.         /// 将中文大写换成阿拉伯数字
  245.         /// </summary>
  246.         /// <param name="strRMB"></param>
  247.         /// <param name="toUpper">true--转换为大写/false--转换为小写</param>
  248.         /// <returns></returns>
  249.         private static string Replace(string strRMB, bool toUpper)
  250.         {
  251.             if (toUpper)
  252.             {
  253.                 strRMB = strRMB.Replace("0""零");
  254.                 strRMB = strRMB.Replace("1""壹");
  255.                 strRMB = strRMB.Replace("2""贰");
  256.                 strRMB = strRMB.Replace("3""叁");
  257.                 strRMB = strRMB.Replace("4""肆");
  258.                 strRMB = strRMB.Replace("5""伍");
  259.                 strRMB = strRMB.Replace("6""陆");
  260.                 strRMB = strRMB.Replace("7""柒");
  261.                 strRMB = strRMB.Replace("8""捌");
  262.                 strRMB = strRMB.Replace("9""玖");
  263.             }
  264.             else
  265.             {
  266.                 strRMB = strRMB.Replace("零""0");
  267.                 strRMB = strRMB.Replace("壹""1");
  268.                 strRMB = strRMB.Replace("贰""2");
  269.                 strRMB = strRMB.Replace("叁""3");
  270.                 strRMB = strRMB.Replace("肆""4");
  271.                 strRMB = strRMB.Replace("伍""5");
  272.                 strRMB = strRMB.Replace("陆""6");
  273.                 strRMB = strRMB.Replace("柒""7");
  274.                 strRMB = strRMB.Replace("捌""8");
  275.                 strRMB = strRMB.Replace("玖""9");
  276.             }
  277.             return strRMB;
  278.         }
  279.         /// <summary>
  280.         /// 获取单位名称
  281.         /// </summary>
  282.         /// <param name="iCode"></param>
  283.         /// <returns></returns>
  284.         private static string GetUnit(int iCode)
  285.         {
  286.             switch (iCode)
  287.             {
  288.                 case -2:
  289.                     return "分";
  290.                 case -1:
  291.                     return "角";
  292.                 case 0:
  293.                     return "元";
  294.                 case 1:
  295.                     return "拾";
  296.                 case 2:
  297.                     return "佰";
  298.                 case 3:
  299.                     return "仟";
  300.                 case 4:
  301.                     return "萬";
  302.                 case 8:
  303.                     return "亿";
  304.                 default:
  305.                     return "";
  306.             }
  307.         }
  308.         /// <summary>
  309.         /// 获取位权指数
  310.         /// </summary>
  311.         /// <param name="cUnit"></param>
  312.         /// <returns></returns>
  313.         private static int GetExp(char cUnit)
  314.         {
  315.             switch (cUnit)
  316.             {
  317.                 case '分':
  318.                     return -2;
  319.                 case '角':
  320.                     return -1;
  321.                 case '元':
  322.                 case '圆':
  323.                     return 0;
  324.                 case '十':
  325.                 case '拾':
  326.                     return 1;
  327.                 case '百':
  328.                 case '佰':
  329.                     return 2;
  330.                 case '千':
  331.                 case '仟':
  332.                     return 3;
  333.                 case '万':
  334.                 case '萬':
  335.                     return 4;
  336.                 case '亿':
  337.                     return 8;
  338.                 default:
  339.                     return 0;
  340.             }
  341.         }
  342.         #endregion
  343.     }
  344. }

你可能感兴趣的:(正则表达式,null)