53

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...
PICyourBrain
  • 9,976
  • 26
  • 91
  • 136

4 Answers4

115

How about using:

decimal d = decimal.Parse("$45.00", NumberStyles.Currency);

The MSDN documentation on Decimal.Parse states:

"The s parameter is interpreted using the NumberStyles.Number style. This means that white space and thousands separators are allowed but currency symbols are not. To explicitly define the elements (such as currency symbols, thousands separators, and white space) that can be present in s, use the Decimal.Parse(String, NumberStyles, IFormatProvider) method

John Koerner
  • 37,428
  • 8
  • 84
  • 134
  • 3
    And if you still want to use your `NumberFormatInfo`: `decimal d = decimal.Parse("$45.00", NumberStyles.Currency, MyNFI);` – Chris Schmich Feb 10 '11 at 03:37
  • 1
    The MSDN documentation on Decimal.Parse states: "The s parameter is interpreted using the NumberStyles.Number style. This means that white space and thousands separators are allowed but currency symbols are not. To explicitly define the elements (such as currency symbols, thousands separators, and white space) that can be present in s, use the Decimal.Parse(String, NumberStyles, IFormatProvider) method." – John Koerner Feb 10 '11 at 03:48
  • 1
    that's good information. It would be worth it to make it part of the answer, it will grab more attention than a comment. – Anthony Pegram Feb 10 '11 at 03:52
  • 1
    `NumberStyles` is in the `System.Globalization` namespace, so either import it: `using System.Globalization` at the top of your .cs file, or specify the full path when you use it: `System.Globalization.NumberStyles`. – northben Apr 22 '13 at 20:29
  • @HristoYankov , can you be more specific about the situation that does not work? I would expect it to use the currency symbols of the current culture. I'd like to write code like this that does work for any culture. – Bruce Dunwiddie Oct 06 '16 at 19:19
19

This way it works for me:

NumberFormatInfo MyNFI = new NumberFormatInfo();
MyNFI.NegativeSign = "-";
MyNFI.CurrencyDecimalSeparator = ".";
MyNFI.CurrencyGroupSeparator = ",";
MyNFI.CurrencySymbol = "$";

decimal d = decimal.Parse("$45.00", NumberStyles.Currency, MyNFI);

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.

MEN
  • 547
  • 6
  • 7
  • 1
    Just wanted to note that this version works on non standard formatting too (so for example, $-2.78 vs. -$2.78) which, to me, seems much more bullet proof – Bob Sheehan Mar 20 '18 at 17:27
2

When I tried to run the code from @JohnKoerner, it would fail with the exception: System.FormatException, with the message: "Input string was not in a correct format.". @MEN's answer was helpful, but I wanted to add some additional insight about the accepted answer and how to fix that problem.

Much like @MEN, I had to include NumberFormatInfo before the .Parse() method worked properly. However, specifying the decimal with CurrencyDecimalSeparator wasn't necessary for me. You'll have to include all the properties you need for your numbers. Here's a list in the class definition docs:

MSDN Docs - NumberFormatInfo Class

I'll never get negative numbers in my implementation, so I chose not to include that. Here's what I have:

string currencyAmount = "$45.00";

NumberFormatInfo FormatInfo = new NumberFormatInfo();
FormatInfo.CurrencyGroupSeparator = ",";
FormatInfo.CurrencySymbol = "$";

// Result: 45.00
decimal parsedCurrency = decimal.Parse(currencyAmount, NumberStyles.Currency, FormatInfo);
kbpontius
  • 3,867
  • 1
  • 30
  • 34
0

Please check regional settings in windows. if currency as in us dollar ($200). Regional format should be in United state.

I am working in India , This changes are work for me. Please correct me if its not correct.

enter image description here

ITsDEv
  • 51
  • 3