人民币大小写转换

来源:互联网 发布:linux运维之道 编辑:程序博客网 时间:2024/05/16 09:22
  1.     static class Money
  2.     {
  3.         private static string[] NumUpper = { "零""壹""贰""叁""肆""伍""陆""柒""捌""玖" };
  4.         private static string[] DanWei = { "1""10""100""1000""10000""100000""1000000""10000000000" };
  5.         private static string[] DanWeiUpper = { "分""角""圆""拾""佰""仟""萬""亿" };
  6.         public static string GetMoneyUpper(decimal money)
  7.         {
  8.             money = (long)(Math.Round(money, 2) * 100);
  9.             string result = GetMoneyUpper(money.ToString());
  10.             string lastDanWei = result.Substring(result.Length - 1, 1);
  11.             if (lastDanWei == "圆")
  12.             {
  13.                 result += "整";
  14.             }
  15.             else if (lastDanWei != "分" && lastDanWei != "角")
  16.             {
  17.                 result += "圆整";
  18.             }
  19.             return result;
  20.         }
  21.         public static string GetMoneyUpper(string money)
  22.         {
  23.             string result = "";
  24.             for (int i = DanWei.Length - 1; i >= 0; i--)
  25.             {
  26.                 string _danWei = DanWei[i];
  27.                 if (money.Length >= _danWei.Length)
  28.                 {
  29.                     string moneyTmp = money.Substring(0, money.Length - _danWei.Length + 1);
  30.                     if (moneyTmp.Length == 1)
  31.                     {
  32.                         if (moneyTmp == "0")
  33.                         {
  34.                             if (DanWeiUpper[i] == "圆")
  35.                                 result += "圆";
  36.                             else
  37.                                 result += NumUpper[int.Parse(moneyTmp)];                    //零不用加单位
  38.                         }
  39.                         else
  40.                         {
  41.                             result += NumUpper[int.Parse(moneyTmp)] + DanWeiUpper[i];       //非零要加单位
  42.                         }
  43.                     }
  44.                     else
  45.                     {
  46.                         result += GetMoneyUpper(moneyTmp + "00");
  47.                         if (result.Substring(result.Length - 1, 1) == "圆" && DanWeiUpper[i] != "圆")
  48.                             result = result.Remove(result.Length - 1);
  49.                         result += DanWeiUpper[i];          //大于玖的数字要进一步实现
  50.                     }
  51.                     money = money.Substring(moneyTmp.Length);
  52.                     //如果已经匹配完,返回
  53.                     if (string.IsNullOrEmpty(money)) break;
  54.                     //如果全是零,则不用表示(例:300;5000)
  55.                     if (int.Parse(money) == 0) break;
  56.                     //计算连续的零的个数,如果超过一个,则压缩成一个;
  57.                     int zeroLen = money.Length - int.Parse(money).ToString().Length;
  58.                     if (zeroLen > 1)
  59.                     {
  60.                         if (int.Parse(money) > 9)
  61.                             money = "0" + int.Parse(money).ToString();
  62.                         else
  63.                             money = "00" + int.Parse(money).ToString();
  64.                         i = i + zeroLen - 1;
  65.                     }
  66.                 }
  67.             }
  68.             return result;
  69.         }
  70.     }
原创粉丝点击