Solved

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

Posted on 2011-03-17
6
253 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now