在C#中将十进制或字符串转换为货币的最佳方法? [英] Best way to convert decimal or string to currency in C#?

查看:98
本文介绍了在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();
        }
   }

这是很好的方法还是我可以做得更好?我从SQL数据库获取数据。我得到十进制值和货币(例如EUR,USD,PLN)。这似乎可行,但也许有更好的选择?还是现在这是单线程应用程序,我是在更改Thread.CurrentThread.CurrentCulture时进行全局更改还是在我从方法返回之前只是临时更改?

Is this good aproach or i could do this better? I get data from SQL database. 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?

With

MadBoy

推荐答案

您可以传递自己想要的文化作为 string.Format 的第一个参数。这比每次更改当前线程要好。您可能还需要设置某种地图或词典,以使您也可以从货币代码映射到文化代码-这将大大减少此处的代码行数。

You can pass the culture you want in as the first parameters to 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);

或者,如果您根据货币代码存储了CultureInfo实例的映射,则将具有以下内容:

Or if you store a map of CultureInfo instances against a currency code, you'd have this:

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

这篇关于在C#中将十进制或字符串转换为货币的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆