码迷,mamicode.com
首页 > Windows程序 > 详细

C# .net实现货币转换示例

时间:2017-11-13 23:14:52      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:point   mat   develop   htm   eve   html   post   net   ant   

本文所述C# .net实现货币转换示例主要利用string.format 和cultureInfo 来进行转换。分享给大家供大家参考之用。具体方法如下:

主要实现代码如下:

/// <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;
}

希望本文所述对大家的C#程序设计有所帮助

除声明外,跑步客文章均为原创,转载请以链接形式标明本文地址
  C# .net实现货币转换示例

本文地址:  http://www.paobuke.com/develop/c-develop/pbk23580.html






相关内容

C# .net实现货币转换示例

标签:point   mat   develop   htm   eve   html   post   net   ant   

原文地址:http://www.cnblogs.com/paobuke/p/7828235.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!