I am trying to parse a string like "$45.59" into a decimal. For some reason I am getting exception that the input was not in the correct format. I don't care about all the localization stuff because this is not going to be a global program. Here is what I am doing. Do you see any problems?
NumberFormatInfo MyNFI = new NumberFormatInfo();
MyNFI.NegativeSign = "-";
MyNFI.NumberDecimalSeparator = ".";
MyNFI.NumberGroupSeparator = ",";
MyNFI.CurrencySymbol = "$";
decimal d = decimal.Parse("$45.00", MyNFI); // throws exception here...
How about using:
The MSDN documentation on Decimal.Parse states:
This way it works for me:
1.) You have to define the currency separator instead of the number separator. 2.) Because you defined the currency values only, you need to define the NumberStyles.Currency while parsing.