We help IT Professionals succeed at work.

decimal.TryParse fails

Camillia
Camillia asked
on
Medium Priority
2,344 Views
Last Modified: 2012-05-06
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.
Comment
Watch Question

You have to specify that the string contains a currency symbol.

If(decimal.TryParse(discount, Globalization.NumberStyles.Currency, Globalization.CultureInfo.CurrentCulture, newDiscount))

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT

Author

Commented:
it contains a $. Letg me try you method now.
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.
CERTIFIED EXPERT

Author

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

http://msdn.microsoft.com/en-us/library/9zbda557.aspx
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    
CERTIFIED EXPERT

Author

Commented:
oh, thanks.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.