18

I have no idea why this is not working:

string s = "12,00 €";
var germanCulture = CultureInfo.CreateSpecificCulture("de-DE");
decimal d;
if (decimal.TryParse(s, NumberStyles.AllowCurrencySymbol, germanCulture, out d))
{
    // i want to get to this point
    Console.WriteLine("Decimal value: {0}", d);
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939

3 Answers3

30

Use NumberStyles.Currency instead of NumberStyles.AllowCurrencySymbol

if (decimal.TryParse(s, NumberStyles.Currency, germanCulture, out d))

and the output for you code would be:

Decimal value: 12.00
Habib
  • 219,104
  • 29
  • 407
  • 436
  • 8
    A little more information to explain why this is so: NumberStyles.AllowCurrencySymbol only permits the currency symbol to be ignored as part of the parsing process. The additional whitespace and decimal separator require additional flags to enable them: AllowDecimalPoint and AllowTrailingWhite. Currency combines these flags (and a few others) [link](http://msdn.microsoft.com/en-us/library/0xh24xh4.aspx) – Eric MSFT Dec 14 '12 at 18:22
0

Try this;

 string.Format(new CultureInfo("en-SG", false), "{0:c0}", 123423.083234);

It will convert 123423.083234 to $1,23,423 format.

Mohan Gopi
  • 237
  • 1
  • 4
  • 12
  • 1
    I needed to parse a string that contains a currency symbol to decimal. You are converting a given decimal to string. So that's a different requirement. However, it is already answered. – Tim Schmelter Aug 29 '13 at 10:18
0

Please note that this doesn't work with the bitcoin symbol and other cryptocurrency symbols yet, unfortunately. I tried both ways.

Sharplab proof: link

A workaround in my case (I have a minus sign followed by the bitcoin ฿ symbol) would be to do a .Where() on string ignoring everything except numbers and minus sign... But if you want general validation... Well then you'll have to hardcode these bitcoin etc symbols somewhere