·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> C#.net之货币转换

C#.net之货币转换

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

利用string.format 和cultureInfo 来进行转换

C#代码  收藏代码
  1. /// <summary>  
  2.         /// 输入Float格式数字,将其转换为货币表达方式  
  3.         /// </summary>  
  4.         /// <param name="ftype">货币表达类型:0=带¥的货币表达方式;1=不带¥的货币表达方式;其它=带¥的货币表达方式</param>  
  5.         /// <param name="fmoney">传入的int数字</param>  
  6.         /// <returns>返回转换的货币表达形式</returns>  
  7.         public string Rmoney(int ftype, double fmoney)  
  8.         {  
  9.             string _rmoney;  
  10.             try  
  11.             {  
  12.                 switch (ftype)  
  13.                 {  
  14.                     case 0:  
  15.                         _rmoney = string.Format("{0:C2}", fmoney);  
  16.                         break;  
  17.   
  18.                     case 1:  
  19.                         _rmoney = string.Format("{0:N2}", fmoney);  
  20.                         break;  
  21.   
  22.                     default:  
  23.                         _rmoney = string.Format("{0:C2}", fmoney);  
  24.                         break;  
  25.                 }  
  26.             }  
  27.             catch  
  28.             {  
  29.                 _rmoney = "";  
  30.             }  
  31.   
  32.             return _rmoney;  
  33.         }  
  34.   
  35.         /// <summary>  
  36.         /// 输入Float格式数字,将其转换为货币表达方式  
  37.         /// </summary>  
  38.         /// <param name="ftype">货币表达类型:0=人民币;1=港币;2=美钞;3=英镑;4=不带货币;其它=不带货币表达方式</param>  
  39.         /// <param name="fmoney">传入的int数字</param>  
  40.         /// <returns>返回转换的货币表达形式</returns>  
  41.         public static string ConvertCurrency(decimal fmoney)  
  42.         {  
  43.             CultureInfo cul = null;  
  44.             int ftype=4;  
  45.             string _rmoney=string.Empty;  
  46.             try  
  47.             {  
  48.                 switch (ftype)  
  49.                 {  
  50.                     case 0:  
  51.                         cul = new CultureInfo("zh-CN");//中国大陆  
  52.                         _rmoney = fmoney.ToString("c", cul);  
  53.                         break;  
  54.   
  55.                     case 1:  
  56.                         cul = new CultureInfo("zh-HK");//香港  
  57.                         _rmoney = fmoney.ToString("c", cul);  
  58.                         break;  
  59.                     case 2:  
  60.                         cul = new CultureInfo("en-US");//美国  
  61.                         _rmoney = fmoney.ToString("c", cul);  
  62.                         break;  
  63.                     case 3:  
  64.                         cul = new CultureInfo("en-GB");//英国  
  65.                         _rmoney = fmoney.ToString("c", cul);  
  66.                         break;  
  67.                     case 4:  
  68.                         _rmoney = string.Format("{0:n}", fmoney);//没有货币符号  
  69.                         break;  
  70.   
  71.                     default:  
  72.                         _rmoney = string.Format("{0:n}", fmoney);  
  73.                         break;  
  74.                 }  
  75.             }  
  76.             catch  
  77.             {  
  78.                 _rmoney = "";  
  79.             }  
  80.   
  81.             return _rmoney;  
  82.         }