c# Convert.ToDouble format exception error
Asked Answered
T

4

9

I'm trying to convert this string to double

Convert.ToDouble("1.12");

and this is the output

System.FormatException was unhandled.

Should I do something like this?

    public static double ConvertToDouble(string ParseVersion)
    {
        double NewestVersion;
        try
        {
            NewestVersion = Convert.ToDouble(ParseVersion);
        }
        catch
        {
            ParseVersion = ParseVersion.Replace('.', ',');
            NewestVersion = Convert.ToDouble(ParseVersion);
        }

        return NewestVersion;
    }

    ConvertToDouble("1.12");

Or is there an easier solution?

Tambac answered 23/7, 2011 at 12:41 Comment(1)
Convert.ToDouble("1.12") works for me. No exception. What culture are you running under?Rutter
F
30

double.Parse will use the current culture by default. It sounds like you want the invariant culture:

double d = double.Parse("1.12", CultureInfo.InvariantCulture);

EDIT: Just to be clear, obviously you shouldn't use this if you're trying to parse text entered by a user in a different culture. This is for use when you've received data in the invariant culture (as most machine-to-machine data text-based formats are) and want to enforce that when parsing.

Firenze answered 23/7, 2011 at 12:49 Comment(6)
it's ok but it will not work for general user text input on many PCsTrochee
double x = Double.Parse("1,12", System.Globalization.NumberFormatInfo.InvariantInfo); Console.WriteLine(x); x = 112 (sic!)Trochee
@Boris: I think you've misunderstood the question. The problem as I understand it is that the OP is on a system where comma is the default user decimal separator - but he's trying to parse a value which isn't in the user culture, but is using "." as the decimal separator.Firenze
If the OP created a standalone method, I think he is looking for some general solution to parse doubles. Only the OP knows )).Trochee
@Boris: Well, it's true that only the OP knows for sure - but we know for absolute sure that he's trying to parse "1.12". You're only guessing that he might also want to parse "1,12". There are plenty of situations in which that simply won't be the case.Firenze
Jon Skeet understand my problem. I want only convert string variable to double. And I want use "." as the decimal separator but I got error because of CultureInfo. So I used double.Parse("1.12", CultureInfo.InvariantCulture);Tambac
C
3

You don't have to replace . to ,.. however a better way is to use the .net TryParse method like:

double d;
if (double.TryParse("your string data", out d)
{
    Console.WriteLine(d);
}

Edit: Also note that by replacing . by , you are getting a wrong results, for instance 1.12:

double d = double.Parse(1.12);//d will equals to 1.12
double d = double.Parse(1,12);//d will equals to 112.0
Canonicity answered 23/7, 2011 at 12:45 Comment(1)
The results of the parsing with a ',' instead of the '.' depends on the ambiant culture. If you want to deploy in multiple cultural context or control the form of your data, you have to pass the culture to the Parse or TryParse method.Northrop
N
2

Convert.ToDouble uses Double.Parse internally. If you are unsure of the culture context, you should use an overload of Double.Parse precising the culture:

double d = double.Parse("1.12", CultureInfo.InvariantCulture);
Northrop answered 23/7, 2011 at 12:49 Comment(0)
W
0

Keep in mind, this problem can depend on where the input string comes from. If it is read from a database as an object, you might solve your problem by keeping it as an object and using Convert.ToDouble() as follows:

public double Double_fromObject(object obj)
    {
      double dNum = 0.0;
      if (obj.ToString() != string.Empty) // the Convert fails when ""
      {
        try
        {
          dNum = Convert.ToDouble(obj);
        }
        catch (SystemException sex)
        {
          // this class's error string
          LastError = sex.Message;
        }
      }

      return (dNum);
    }
Woolgrower answered 12/8, 2014 at 15:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.