C# .net實現(xiàn)貨幣轉(zhuǎn)換示例
更新時間:2014年08月27日 15:10:49 投稿:shichen2014
這篇文章主要介紹了C# .net實現(xiàn)貨幣轉(zhuǎn)換,其中包含了try catch、switch語句的運用,對于C#初學者有一定的借鑒價值,需要的朋友可以參考下
本文所述C# .net實現(xiàn)貨幣轉(zhuǎn)換示例主要利用string.format 和cultureInfo 來進行轉(zhuǎn)換。分享給大家供大家參考之用。具體方法如下:
主要實現(xiàn)代碼如下:
/// <summary>
/// 輸入Float格式數(shù)字,將其轉(zhuǎn)換為貨幣表達方式
/// </summary>
/// <param name="ftype">貨幣表達類型:0=帶¥的貨幣表達方式;1=不帶¥的貨幣表達方式;其它=帶¥的貨幣表達方式</param>
/// <param name="fmoney">傳入的int數(shù)字</param>
/// <returns>返回轉(zhuǎn)換的貨幣表達形式</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格式數(shù)字,將其轉(zhuǎn)換為貨幣表達方式
/// </summary>
/// <param name="ftype">貨幣表達類型:0=人民幣;1=港幣;2=美鈔;3=英鎊;4=不帶貨幣;其它=不帶貨幣表達方式</param>
/// <param name="fmoney">傳入的int數(shù)字</param>
/// <returns>返回轉(zhuǎn)換的貨幣表達形式</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#程序設(shè)計有所幫助
您可能感興趣的文章:
- C#實現(xiàn)將浮點數(shù)表示的貨幣數(shù)量以漢字大寫形式輸出的方法
- 使用C#實現(xiàn)阿拉伯數(shù)字到大寫中文的轉(zhuǎn)換
- c#中判斷字符串是不是數(shù)字或字母的方法
- C#識別出圖片里的數(shù)字和字母
- C# 判斷字符串第一位是否為數(shù)字
- C#實現(xiàn)將千分位字符串轉(zhuǎn)換成數(shù)字的方法
- C#實現(xiàn)大數(shù)字運算的實例代碼
- c#實現(xiàn)識別圖片上的驗證碼數(shù)字
- c#中文轉(zhuǎn)unicode字符示例分享
- C# 中文簡體轉(zhuǎn)繁體實現(xiàn)代碼
- C#基于純數(shù)學方法遞歸實現(xiàn)貨幣數(shù)字轉(zhuǎn)換中文功能詳解
相關(guān)文章
c#檢測usb設(shè)備撥插類庫USBClassLibrary分享
這篇文章主要介紹了c#檢測usb設(shè)備撥插類庫USBClassLibrary的簡單示例,需要的朋友可以參考下2014-04-04
C#函數(shù)式程序設(shè)計之用閉包封裝數(shù)據(jù)的實現(xiàn)代碼
如果一個程序設(shè)計語言能夠用高階函數(shù)解決問題,則意味著數(shù)據(jù)作用域問題已十分突出。當函數(shù)可以當成參數(shù)和返回值在函數(shù)之間進行傳遞時,編譯器利用閉包擴展變量的作用域,以保證隨時能得到所需要的數(shù)據(jù)2014-03-03

