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
Solved

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

Posted on 2011-03-17
6
285 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
When there is a disconnect between the intentions of their creator and the recipient, when algorithms go awry, they can have disastrous consequences.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
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…

840 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