我一直在試圖找到最好的方式來將十進制/字符串轉換爲貨幣取決於我的選擇。在C#中將十進制或字符串轉換爲貨幣的最佳方式是什麼?
public static string returnWaluta(string varS, string varSymbol) {
decimal varD = decimal.Parse(varS);
if (varSymbol == "EUR") {
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR", false);
return String.Format("{0:c}", varD);
} else if (varSymbol == "PLN") {
Thread.CurrentThread.CurrentCulture = new CultureInfo("pl-PL", false);
return String.Format("{0:c}", varD);
} else if (varSymbol == "USD") {
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false);
return String.Format("{0:c}", varD);
} else {
// Not handled currency
MessageBox.Show(varSymbol);
return varS.ToString();
}
}
public static string returnWaluta(decimal varS, string varSymbol) {
if (varSymbol == "EUR") {
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR", false);
return String.Format("{0:c}", varS);
} else if (varSymbol == "PLN") {
Thread.CurrentThread.CurrentCulture = new CultureInfo("pl-PL", false);
return String.Format("{0:c}", varS);
} else if (varSymbol == "USD") {
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false);
return String.Format("{0:c}", varS);
} else {
// Not handled currency
MessageBox.Show(varSymbol);
return varS.ToString();
}
}
這是好的方法還是我可以做得更好?我從SQL數據庫中獲取數據。我獲得十進制值和貨幣(如EUR,USD,PLN)。這似乎工作,但也許有更好的選擇?也是現在,這是單線程應用程序,我是否在改變Thread.CurrentThread.CurrentCulture時進行全局更改,還是隻是暫時的,直到我從方法返回?
與問候,
MadBoy
它傷害了我的眼睛! – 2010-01-19 12:40:34