Problem parsing currency text to decimal type

C#ParsingDecimalNumber Formatting

C# Problem Overview


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...

C# Solutions


Solution 1 - C#

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

Solution 2 - C#

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.

Solution 3 - C#

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);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPICyourBrainView Question on Stackoverflow
Solution 1 - C#John KoernerView Answer on Stackoverflow
Solution 2 - C#MENView Answer on Stackoverflow
Solution 3 - C#kbpontiusView Answer on Stackoverflow