Solved

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

Posted on 2011-03-17
6
278 Views
Last Modified: 2012-05-11
What's the most concise method to "convert" a string ($200.00) to a double -200.00 in C#?
0
Comment
Question by:dgerler
  • 3
  • 2
6 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 35158371
Try this:

double value = Double.Parse("($200.00)", System.Globalization.NumberStyles.Currency);
0
 
LVL 8

Expert Comment

by:mac-will
ID: 35158378
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
 

Author Closing Comment

by:dgerler
ID: 35158689
Very concise! Thank you.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 8

Expert Comment

by:mac-will
ID: 35158697
Don't forget to catch the exception if it's bad data otherwise use the tryparse method.
0
 

Author Comment

by:dgerler
ID: 35158943
Yep. Was catching the exception in a general sense, but wanted a nice elegant and concise way rather than using several .replace() methods.
0
 
LVL 8

Expert Comment

by:mac-will
ID: 35165390
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# bracket error 3 32
tutorial for ebay api 3 35
Reading the web config for a running service on Windows 10 16 42
Problem!!! 8 33
Article by: Nadia
Suppose you use Uber application as a rider and you request a ride to go from one place to another. Your driver just arrived at the parking lot of your place. The only thing you know about the ride is the license plate number. How do you find your U…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 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