Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Convert.ToDouble and double.Parse in correlation with InvariantCulture

I am wondering why this is working:

doubleValue = double.Parse(input[0].ToString(System.Globalization.CultureInfo.InvariantCulture).Replace(',', '.'), System.Globalization.CultureInfo.InvariantCulture);

while this isn't:

doubleValue = Convert.ToDouble(input[0])

The point is, there are about 30 machines in one country (same Windows image, same hardware, different location). While the first 20 machines are fine with Convert.ToDouble(), the 10 other ones can't convert the values properly (They loose the decimal point in every case, no matter if point or comma).

Since the program is really big an complex, is there an opportunity to get Convert.ToDouble() working without changing the program itself?

Another point is, i tried different methods to convert my string value to a double, none of them are working but only the double.Parse()...

And also, is it generally bad to use Convert.ToDouble() vor strings? (Only for objects)

Edit:

I created this method inside my class:

public static double ToDouble(string value, IFormatProvider provider)
{
    if (value == null)
    {
        return 0.0;
    }

    return double.Parse(value, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands | NumberStyles.AllowExponent, provider);
}

and called it with (tried also points and commas):

doubleValue = ToDouble(myTextBox.Text, CultureInfo.InvariantCulture);

Result: Still not working...

like image 662
Essigwurst Avatar asked Oct 05 '17 06:10

Essigwurst


People also ask

What does double parse mean?

Description. Double Parse(String, NumberStyles, IFormatProvider) converts the string representation of a number in a specified style and culture-specific format to its double-precision floating-point number equivalent.

How do I convert a string to a double in C#?

String value can be converted to double using Convert. ToDouble() or Double. Parse() method. These methods take string representation of a number as input and return its equivalent double-precision floating-point number.


3 Answers

As I remember, Convert.ToDouble() looks like this:

// System.Convert
public static double ToDouble(string value)
{
    if (value == null)
    {
        return 0.0;
    }
    return double.Parse(value, CultureInfo.CurrentCulture);
}

As you can see, internally it calls double.Parse() method, with CurrentCulture. So if you've got a string, and you expect it to always be a double, use double.Parse() with the culture you prefer.

P.S. Yeap, I was right, you can look in mscorlib.dll with ILSpy.

P.P.S I forgot about ReferenceSource resource, so you can find out same thing Here.

like image 155
SᴇM Avatar answered Oct 05 '22 06:10

SᴇM


Convert.ToDouble uses current thread culture so you cannot specify explicitly the culture you want to cast.

Whereas double.Parse provides you an overload to specify the culture you want to parse into, that's the reason why Convert.ToDouble is not working on some of your machines.

like image 42
Ipsit Gaur Avatar answered Oct 05 '22 07:10

Ipsit Gaur


For anyone who run into such a problem, i finally figured out what the problem was.

Background: Some local IT guy installed a database client, which swapped the "decimal symbol" with the "digit grouping symbol" at the windows "region and language" settings.

However, the region / language itself wasn't changed, so it seems the program run into troubles here.

Replacing the "." with "," may additionaly cause problems if the string already contains a ".".

This problem could also be solved by asking the CurrentCulture, NumberDecimalSeparator, and the NumbergroupSeparator properties and handle the string specifilcly then.

like image 22
Essigwurst Avatar answered Oct 05 '22 07:10

Essigwurst