Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse value with Currency symbol

Tags:

c#

.net

parsing

I have looked to multiple SO questions on parsing currency, the best (recommended) way seems to be the one I'm trying below:

var payout = decimal.Parse("$2.10", NumberStyles.Currency | NumberStyles.AllowDecimalPoint);

However, it throws and exception: Input string is not in the correct format.

I don't know what I'm doing wrong?

EDIT

Thanks for the answers. Additional info: the hard-coded currency value I gave was just an example. I have a list of currencies:

€2,66

$2.10

$5.55

etc.

I cannot determine the culture info in advance. Any ideas?

like image 758
l3utterfly Avatar asked Nov 04 '25 16:11

l3utterfly


2 Answers

You can try like this:

decimal currencyValue;
string inputCurrency = "$12.6";
if (decimal.TryParse(inputCurrency, NumberStyles.Currency, CultureInfo.CreateSpecificCulture("en-US"), out currencyValue))
  {
      // proceed with currencyValue
  }
else 
  {
      //Show error ; Conversion failed
  }

For dealing with all currencies you can use the following:

        Dictionary<char, string> currencyCulture = new Dictionary<char, string>();
        currencyCulture.Add('$', "en-US");
        currencyCulture.Add('€', "en-IE");
        // populate all posible values here
        decimal currencyValue;
        string inputCurrency = "€2,66";
        char currencySymbol= inputCurrency.ToCharArray()[0];
        CultureInfo currentCulture= CultureInfo.CreateSpecificCulture(currencyCulture[currencySymbol]);
        if (decimal.TryParse(inputCurrency, NumberStyles.Currency, currentCulture, out currencyValue))
        {
            // proceed with currencyValue
        }
        else 
        {
         //Show error ; Conversion failed
        }

You can choose culture Names from here

like image 134
sujith karivelil Avatar answered Nov 06 '25 07:11

sujith karivelil


Similar approach @un-lucky mentioned as one of the answer, I tried making it generic and work for every Symbol/Format

public static decimal ParseCurrencyWithSymbol(string input)
{
    var cultures = CultureInfo.GetCultures(CultureTypes.AllCultures)
        .GroupBy(c=> c.NumberFormat.CurrencySymbol)
        .ToDictionary(c=> c.Key, c=>c.First());


    var culture = cultures.FirstOrDefault(c=>input.Contains(c.Key));

    decimal result = 0;
    if(!culture.Equals(default(KeyValuePair<string,CultureInfo>)))
    {
        result = decimal.Parse(input, NumberStyles.Currency | NumberStyles.AllowDecimalPoint, culture.Value);
    }
    else
    {
        if( !decimal.TryParse(input, out result))
        {
            throw new Exception("Invalid number format");
        }
    }

    return result;
}

Usage

decimal output = ParseCurrencyWithSymbol("$2.10");

Working Code

like image 41
Hari Prasad Avatar answered Nov 06 '25 07:11

Hari Prasad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!