C#转换人民币大小金额
更新:HHH   时间:2023-1-7


 

  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.         } 

 

返回开发技术教程...