Home  >  Article  >  Backend Development  >  C# .net implementation of currency conversion example

C# .net implementation of currency conversion example

高洛峰
高洛峰Original
2017-01-16 10:43:062148browse

The C# .net currency conversion example described in this article mainly uses string.format and cultureInfo for conversion. Share it with everyone for your reference. The specific method is as follows:

The main implementation code is as follows:

/// <summary> 
/// 输入Float格式数字,将其转换为货币表达方式 
/// </summary> 
/// <param name="ftype">货币表达类型:0=带¥的货币表达方式;1=不带¥的货币表达方式;其它=带¥的货币表达方式</param> 
/// <param name="fmoney">传入的int数字</param> 
/// <returns>返回转换的货币表达形式</returns> 
public string Rmoney(int ftype, double fmoney) 
{ 
  string _rmoney; 
  try
  { 
 switch (ftype) 
 { 
   case 0: 
 _rmoney = string.Format("{0:C2}", fmoney); 
 break; 
   case 1: 
 _rmoney = string.Format("{0:N2}", fmoney); 
 break; 
   default: 
 _rmoney = string.Format("{0:C2}", fmoney); 
 break; 
 } 
  } 
  catch
  { 
 _rmoney = ""; 
  } 
  return _rmoney; 
} 
/// <summary> 
/// 输入Float格式数字,将其转换为货币表达方式 
/// </summary> 
/// <param name="ftype">货币表达类型:0=人民币;1=港币;2=美钞;3=英镑;4=不带货币;其它=不带货币表达方式</param> 
/// <param name="fmoney">传入的int数字</param> 
/// <returns>返回转换的货币表达形式</returns> 
public static string ConvertCurrency(decimal fmoney) 
{ 
  CultureInfo cul = null; 
  int ftype=4; 
  string _rmoney=string.Empty; 
  try
  { 
 switch (ftype) 
 { 
   case 0: 
 cul = new CultureInfo("zh-CN");//中国大陆 
 _rmoney = fmoney.ToString("c", cul); 
 break; 
   case 1: 
 cul = new CultureInfo("zh-HK");//香港 
 _rmoney = fmoney.ToString("c", cul); 
 break; 
   case 2: 
 cul = new CultureInfo("en-US");//美国 
 _rmoney = fmoney.ToString("c", cul); 
 break; 
   case 3: 
 cul = new CultureInfo("en-GB");//英国 
 _rmoney = fmoney.ToString("c", cul); 
 break; 
   case 4: 
 _rmoney = string.Format("{0:n}", fmoney);//没有货币符号 
 break; 
 
   default: 
 _rmoney = string.Format("{0:n}", fmoney); 
 break; 
 } 
  } 
  catch
  { 
 _rmoney = ""; 
  } 
  return _rmoney; 
}

I hope this article will be helpful to everyone’s C# programming

More C# .net currency conversion examples For related articles, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn