Html5移动开发论坛

注册

 

发新话题 回复该主题

C#数字转大写金额 [复制链接]

1#
  1. #region 数字转大写中文

  2.         /// <summary>
  3.         /// 转大写金额
  4.         /// </summary>
  5.         /// <param name="d"></param>
  6.         /// <returns></returns>
  7.         public static string NumToUpper(decimal d) {
  8.             if (d == 0)
  9.                 return "零元";

  10.             string je = d.ToString("####.00");
  11.             if (je.Length > 15)
  12.                 return "";
  13.             je = new String('0', 15 - je.Length) + je;      //若小于15位长,前面补0

  14.             string stry = je.Substring(0, 4);        //取得'亿'单元
  15.             string strw = je.Substring(4, 4);        //取得'万'单元
  16.             string strg = je.Substring(8, 4);        //取得'元'单元
  17.             string strf = je.Substring(13, 2);        //取得小数部分

  18.             string str1 = "", str2 = "", str3 = "";

  19.             str1 = getupper(stry, "亿");        //亿单元的大写
  20.             str2 = getupper(strw, "万");        //万单元的大写
  21.             str3 = getupper(strg, "元");        //元单元的大写

  22.             string str_y = "", str_w = "";
  23.             if (je[3] == '0' || je[4] == '0')        //亿和万之间是否有0
  24.                 str_y = "零";
  25.             if (je[7] == '0' || je[8] == '0')        //万和元之间是否有0
  26.                 str_w = "零";

  27.             string ret = str1 + str_y + str2 + str_w + str3;    //亿,万,元的三个大写合并

  28.             for (int i = 0; i < ret.Length; i++)        //去掉前面的"零"  
  29.             {
  30.                 if (ret[i] != '零') {
  31.                     ret = ret.Substring(i);
  32.                     break;
  33.                 }

  34.             }

  35.             for (int i = ret.Length - 1; i > -1; i--)      //去掉最后的"零"
  36.             {
  37.                 if (ret[i] != '零') {
  38.                     ret = ret.Substring(0, i + 1);
  39.                     break;
  40.                 }
  41.             }

  42.             if (ret[ret.Length - 1] != '元')        //若最后不位不是'元',则加一个'元'字
  43.                 ret = ret + "元";

  44.             if (ret == "零零元")           //若为零元,则去掉"元数",结果只要小数部分
  45.                 ret = "";

  46.             if (strf == "00")            //下面是小数部分的转换
  47.             {
  48.                 ret = ret + "整";
  49.             } else {
  50.                 string tmp = "";
  51.                 tmp = getint(strf[0]);
  52.                 if (tmp == "零")
  53.                     ret = ret + tmp;
  54.                 else
  55.                     ret = ret + tmp + "角";
  56.                 tmp = getint(strf[1]);
  57.                 if (tmp != "零")
  58.                     ret = ret + tmp + "分";
  59.             }

  60.             if (ret[0] == '零') {
  61.                 ret = ret.Substring(1);          //防止0.03转为"零叁分",而直接转为"叁分"
  62.             }

  63.             return ret;             //完成,返回


  64.         }
复制代码
分享 转发

Sencha Touch 交流 QQ 群 224711028 欢迎您的加入。
PhoneGap安卓 交流 QQ 群 250395324欢迎您的加入。
Html5 APP 交流 QQ 群 273843464欢迎您的加入。
TOP
2#

学习了
TOP
发新话题 回复该主题