Solved

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

Posted on 2011-03-17
6
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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