Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

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#?
0
dgerler
Asked:
dgerler
  • 3
  • 2
1 Solution
 
wdosanjosCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now