简体   繁体   English

在C#中将十进制或字符串转换为货币的最佳方法?

[英]Best way to convert decimal or string to currency in C#?

I've been trying to find best way to convert decimal/string to currency depending on my choice. 我一直试图找到最好的方法将十进制/字符串转换为货币取决于我的选择。

        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();
        }
   }

Is this good aproach or i could do this better? 这是好的方法,还是我可以做得更好? I get data from SQL database. 我从SQL数据库中获取数据。 I get decimal value and currency it is on (like EUR, USD, PLN). 我得到十进制值和货币(如欧元,美元,波兰兹罗提)。 This seems to work but maybe there's better option? 这似乎有效,但也许有更好的选择? Also for now this is single threaded aplication, am i making global change when i change Thread.CurrentThread.CurrentCulture or is it just temporary until i return from the method? 另外现在这是单线程应用程序,当我更改Thread.CurrentThread.CurrentCulture时,我是进行全局更改还是暂时直到我从方法返回?

With regards, 带着敬意,

MadBoy 愤怒的少年

You can pass the culture you want in as the first parameters to string.Format . 您可以将所需的文化作为第一个参数传递给string.Format That would be better than making the change to the current thread every time. 这比每次更改当前线程更好。 You may want to set up some sort of map or dictionary that makes the mapping from a currency code to a culture code for you as well - this would greatly reduce the number of lines of code here. 您可能想要设置某种地图或字典,从而为您创建从货币代码到文化代码的映射 - 这将大大减少此处的代码行数。

return string.Format(new CultureInfo(map[currencyCode], false), "{0:c}", varD);

Or if you store a map of CultureInfo instances against a currency code, you'd have this: 或者,如果您根据货币代码存储CultureInfo实例的地图,您将拥有:

return string.Format(cultureMap[currencyCode], "{0:c}", varD);
Convert.ToDecimal()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM