温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#转换人民币大小金额

发布时间:2020-06-20 19:53:00 来源:网络 阅读:485 作者:linzheng 栏目:开发技术

 

  1. /// <summary>   
  2.         /// 转换人民币大小金额   
  3.         /// </summary>   
  4.         /// <param name="num">金额</param>   
  5.         /// <returns>返回大写形式</returns>   
  6.         public static string GetChineseMoney(decimal num)  
  7.         {  
  8.             string str1 = "零壹贰叁肆伍陆柒捌玖";            //0-9所对应的汉字   
  9.             string str2 = "万仟佰拾亿仟佰拾万仟佰拾元角分"//数字位所对应的汉字   
  10.             string str3 = "";    //从原num值中取出的值   
  11.             string str4 = "";    //数字的字符串形式   
  12.             string str5 = "";  //人民币大写金额形式   
  13.             int i;    //循环变量   
  14.             int j;    //num的值乘以100的字符串长度   
  15.             string ch2 = "";    //数字的汉语读法   
  16.             string ch3 = "";    //数字位的汉字读法   
  17.             int nzero = 0;  //用来计算连续的零值是几个   
  18.             int temp;            //从原num值中取出的值   
  19.  
  20.             if (DbUtils.Language.ToLower() == "en"return "";  
  21.             num = Math.Round(Math.Abs(num), 2);    //将num取绝对值并四舍五入取2位小数   
  22.             str4 = ((long)(num * 100)).ToString();        //将num乘100并转换成字符串形式   
  23.             j = str4.Length;      //找出最高位   
  24.             if (j > 15) { return "溢出"; }  
  25.             str2 = str2.Substring(15 - j);   //取出对应位数的str2的值。如:200.55,j为5所以str2=佰拾元角分   
  26.  
  27.             //循环取出每一位需要转换的值   
  28.             for (i = 0; i < j; i++)  
  29.             {  
  30.                 str3 = str4.Substring(i, 1);          //取出需转换的某一位的值   
  31.                 temp = Convert.ToInt32(str3);      //转换为数字   
  32.                 if (i != (j - 3) && i != (j - 7) && i != (j - 11) && i != (j - 15))  
  33.                 {  
  34.                     //当所取位数不为元、万、亿、万亿上的数字时   
  35.                     if (str3 == "0")  
  36.                     {  
  37.                         ch2 = "";  
  38.                         ch3 = "";  
  39.                         nzero = nzero + 1;  
  40.                     }  
  41.                     else 
  42.                     {  
  43.                         if (str3 != "0" && nzero != 0)  
  44.                         {  
  45.                             ch2 = "零" + str1.Substring(temp * 1, 1);  
  46.                             ch3 = str2.Substring(i, 1);  
  47.                             nzero = 0;  
  48.                         }  
  49.                         else 
  50.                         {  
  51.                             ch2 = str1.Substring(temp * 1, 1);  
  52.                             ch3 = str2.Substring(i, 1);  
  53.                             nzero = 0;  
  54.                         }  
  55.                     }  
  56.                 }  
  57.                 else 
  58.                 {  
  59.                     //该位是万亿,亿,万,元位等关键位   
  60.                     if (str3 != "0" && nzero != 0)  
  61.                     {  
  62.                         ch2 = "零" + str1.Substring(temp * 1, 1);  
  63.                         ch3 = str2.Substring(i, 1);  
  64.                         nzero = 0;  
  65.                     }  
  66.                     else 
  67.                     {  
  68.                         if (str3 != "0" && nzero == 0)  
  69.                         {  
  70.                             ch2 = str1.Substring(temp * 1, 1);  
  71.                             ch3 = str2.Substring(i, 1);  
  72.                             nzero = 0;  
  73.                         }  
  74.                         else 
  75.                         {  
  76.                             if (str3 == "0" && nzero >= 3)  
  77.                             {  
  78.                                 ch2 = "";  
  79.                                 ch3 = "";  
  80.                                 nzero = nzero + 1;  
  81.                             }  
  82.                             else 
  83.                             {  
  84.                                 if (j >= 11)  
  85.                                 {  
  86.                                     ch2 = "";  
  87.                                     nzero = nzero + 1;  
  88.                                 }  
  89.                                 else 
  90.                                 {  
  91.                                     ch2 = "";  
  92.                                     ch3 = str2.Substring(i, 1);  
  93.                                     nzero = nzero + 1;  
  94.                                 }  
  95.                             }  
  96.                         }  
  97.                     }  
  98.                 }  
  99.                 if (i == (j - 11) || i == (j - 3))  
  100.                 {  
  101.                     //如果该位是亿位或元位,则必须写上   
  102.                     ch3 = str2.Substring(i, 1);  
  103.                 }  
  104.                 str5 = str5 + ch2 + ch3;  
  105.  
  106.                 if (i == j - 1 && str3 == "0")  
  107.                 {  
  108.                     //最后一位(分)为0时,加上“整”   
  109.                     str5 = str5 + '整';  
  110.                 }  
  111.             }  
  112.             if (num == 0)  
  113.             {  
  114.                 str5 = "零元整";  
  115.             }  
  116.             return str5;  
  117.         } 

 

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI