人民币大小写格式转换

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值