开发者

What is the best way to convert any currency into decimal in C#?

开发者 https://www.devze.com 2023-01-05 21:32 出处:网络
What is the best way to co开发者_如何学Pythonnvert any currency into decimal in C#? public static decimal returnDecimalFromCurrency(string dataToCheck) {

What is the best way to co开发者_如何学Pythonnvert any currency into decimal in C#?

public static decimal returnDecimalFromCurrency(string dataToCheck) {

  decimal varValue;
  if (!Decimal.TryParse(dataToCheck, NumberStyles.Number | NumberStyles.AllowCurrencySymbol, CultureInfo.CurrentCulture, out varValue)) {
    varValue = decimal.MinValue;
  }
  return varValue;
}

But this one does work for only my current currency. If i pass it EURO currency it won't get converted.

Or the only way is to always check what currency it is and pass proper CultureInfo:

CultureInfo.GetCultureInfo("pl-PL") for zl, or CultureInfo.GetCultureInfo("en-GB") for pounds?

Edit:

Or maybe this is the quick "working" hack?

 Decimal.TryParse(dataToCheck.Trim().Replace("zł", "").Replace("€", ""), NumberStyles.Number | NumberStyles.AllowCurrencySymbol, CultureInfo.GetCultureInfo("pl-PL"), out varValue)

But to be honest I don't really like it.


I'd avoid trying to parse the currency with Decimal.TryParse and instead strip off the currency - giving the resultant string to TryParse. Deal with the currency separately - of course, create a function to do this if you need to do it more than once.


Here 's what works for me, I hope it helps you too:

    public static decimal Parse(string currencyString)
    {
        return Parse(currencyString, Thread.CurrentThread.CurrentCulture);
    }

    public static decimal Parse(string currencyString, CultureInfo culture)
    {
        currencyString = currencyString.Replace(culture.NumberFormat.CurrencySymbol, System.String.Empty);
        var value = Convert.ToDecimal(currencyString, culture);
        return value;
    }


I solved this same problem with a hybrid approach. The regex to ignore period, comma, parenthesis (in my case I know negatives are in parenthesis), and digits is fairly simple. Just use that to find your symbol, and then build a culture from it. Then parse.

//[^\d\.,()]+ all non-digit, non-period, non-comma, non-parenthesis chars
var regex = new Regex("[^\\d\\.,()]+");
var match = regex.Match(currencyString);

if(match == null){ //throw exception }

var currencySymbol = match.Value;

var cultureInfo = Thread.CurrentThread.CurrentCulture.Clone() as CultureInfo;
cultureInfo.NumberFormat.CurrencySymbol = currencySymbol;

var decimalValue = Decimal.Parse(currencyString, NumberStyles.Currency, cultureInfo);


I ran into this issue today, and I ended up just looping through all of the cultures, and passing each one to the TryParse function.

var myString = "$400.01";
var result = 0.0m;

var culture = System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures)
    .First(c => decimal.TryParse(myString, System.Globalization.NumberStyles.Currency, c, out value))

// result = 400.01m
0

精彩评论

暂无评论...
验证码 换一张
取 消