Best way to parsing a negative number from a string in format ($200.00)

What's the most concise method to "convert" a string ($200.00) to a double -200.00 in C#?
dgerlerAsked:
Who is Participating?
 
wdosanjosConnect With a Mentor Commented:
Try this:

double value = Double.Parse("($200.00)", System.Globalization.NumberStyles.Currency);
0
 
mac-willCommented:
String s = "$200.00";

s = s.Replace("$","");

double s_d = 0;

if(double.TryParse(s, out s_d))
{
     s_d *= -1; // make negative

     // do what you need with -200.0

}

Open in new window

0
 
dgerlerAuthor Commented:
Very concise! Thank you.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
mac-willCommented:
Don't forget to catch the exception if it's bad data otherwise use the tryparse method.
0
 
dgerlerAuthor Commented:
Yep. Was catching the exception in a general sense, but wanted a nice elegant and concise way rather than using several .replace() methods.
0
 
mac-willCommented:
No use ths original solution but with tryparse I mean.

Something like:

double value = 0;

if(Double.TryParse("($200.00)", System.Globalization.NumberStyles.Currency, out value))
{

}

Open in new window

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.