decimal.TryParse fails

I have this line of code and I step thru it and it fails. Not sure why:

if (decimal.TryParse(discount, out newDiscount))

"discount" is string and it's $3.50
newDiscount is Decimal.

The if fails and always goes to the else.
LVL 8
CamilliaAsked:
Who is Participating?
 
aibusinesssolutionsCommented:
You have to specify that the string contains a currency symbol.

If(decimal.TryParse(discount, Globalization.NumberStyles.Currency, Globalization.CultureInfo.CurrentCulture, newDiscount))
0
 
CamilliaAuthor Commented:
it contains a $. Letg me try you method now.
0
 
aibusinesssolutionsCommented:
The normal TryParse function doesn't work with currency.  If it see's anything other than numbers in the string, it will not work.  Which is why you have to specify NumberStyles.Currency.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
CamilliaAuthor Commented:
your method worked.  Examples here show that my method should've worked as well.

http://msdn.microsoft.com/en-us/library/9zbda557.aspx
0
 
aibusinesssolutionsCommented:
No, if you look at the output below the examples, it shows this:
// The example displays the following output to the console:
//       1643.57
//       Unable to parse '$1,643.57'.
//       Unable to parse '-1.643e6'.
//       -1689346178821    
0
 
CamilliaAuthor Commented:
oh, thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.