Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Convert.ToDouble format exception error

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?

like image 899
sczdavos Avatar asked Jul 23 '11 12:07

sczdavos


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


3 Answers

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
like image 32
Jalal Said Avatar answered Oct 14 '22 06:10

Jalal Said


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);
like image 39
Falanwe Avatar answered Oct 14 '22 06:10

Falanwe


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.

like image 168
Jon Skeet Avatar answered Oct 14 '22 04:10

Jon Skeet