[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

decimal.TryParse fails

Posted on 2009-02-14
6
Medium Priority
?
2,260 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.
0
Comment
Question by:Camillia
  • 3
  • 3
6 Comments
 
LVL 15

Accepted Solution

by:
aibusinesssolutions earned 2000 total points
ID: 23643571
You have to specify that the string contains a currency symbol.

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

Author Comment

by:Camillia
ID: 23643580
it contains a $. Letg me try you method now.
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23643586
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 7

Author Comment

by:Camillia
ID: 23643599
your method worked.  Examples here show that my method should've worked as well.

http://msdn.microsoft.com/en-us/library/9zbda557.aspx
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23643626
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
 
LVL 7

Author Comment

by:Camillia
ID: 23643639
oh, thanks.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question