1

I am trying to convert a _string[1] to double and _string[2] to Int

this string array is generated dynamically.

string value can be empty or 1.1 or 1 or .1 how can i handle this.

i trying doing like this.

string locale;
locale = System.Web.HttpContext.Current.Request.UserLanguages[0];
CultureInfo culture;
culture = new CultureInfo(locale);
 double cValue = Double.Parse(_string[1], culture.NumberFormat)
int sValue = Int32.Parse(_string[2], culture.NumberFormat)

this sometime give me invalid input when there is empty string or decimal string

2
  • How should an empty string be parsed to a double?! What should that give? Commented Mar 16, 2012 at 21:25
  • for empty or invalid input parse double to 0.00 and for int just 0 Commented Mar 16, 2012 at 21:26

3 Answers 3

6

You can use double.TryParse.

// There's no need to initialize cValue since it's used as an 
// out parameter by TryParse which guarantees initialization.
// If TryParse fails the output parameter will be set it to 
// default(T), where T is double in this case, i.e. 0.

double cValue; 

if( Double.TryParse( line[8], out cValue ) )
{
    // success (cValue is now the parsed value)
}
else
{
    // failure (cValue is now 0)
}

or if you need to specify the culture

if(double.TryParse(line[8], NumberStyles.Any, CultureInfo.CurrentCulture, out cValue))
{
}

If you really want to be concise then you can simply use this:

double cValue;
Double.TryParse( line[8], out cValue );

The extra lines above were just for demonstration.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I choose to go this way
5

For the double you could use a ternary operator like this

double d = Double.TryParse(_string[1], out d) ? Convert.ToDouble(_string[1]) : 0;

In order to make it safe you could either use a try-catch or the Double.TryParse method which is a better option.

If you want to display this, you will get 0 as an output. You can turn that into a 0.00 with the following line

string output = (String.Format("{0:0.00}", cValue));

3 Comments

Unfortunately Convert.ToDouble will throw an exception if the input doesn't represent a valid number, whereas TryParse won't.
@HaBo. Ok, I see. However your code is inefficient. On success or failure you do TryParse and also Convert.ToDouble both of which succeed or fail. There's no point, a single TryParse is all you need.
I've completely missed that point. I thought that TryParse would only try to parse and return a bool if it CAN be parsed. Inefficient but still a working one liner.
0

Try this...

double cValue = 0.0;
int sValue = 0;

if(!String.IsNullOrEmpty(_string[1]))
{
   cValue = Convert.ToDouble(_string[1]);
}

if (!String.IsNullOrEmpty(_string[2]))
{
    sValue = Convert.ToInt32(_string[2]);
}  

http://msdn.microsoft.com/en-us/library/zh1hkw6k.aspx

If your string is null or empty it wont try to convert it. It will just be 0.0 or 0.

4 Comments

But my case is if string is empty?
But it will throw an exception if fails, your choice mostly depends on how you want to do error handling. String empty will throw an exception, converting that case to 0 will always require an extra check.
Doesn't handle empty string.
Changed to handle empty strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.