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

3

9

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...

Yalu answered 5/10, 2017 at 6:39 Comment(10)
"i tried different methods ..." Including Convert.ToDouble(text,CultureInfo.InvariantCulture) ?Prismatoid
You can either change your program or take control over the Regional settings of those 10 other PCs.Prismatoid
Yeah, i tried this for example: doubleValue = Convert.ToDouble(myTextBox.Text.Replace('.', ','), System.Globalization.CultureInfo.InvariantCulture);. Also, it does not matter if it's point to comma or the other way round...Yalu
Which "Regional Settings" do you mean? They are the same. Or am i missing something?Yalu
Can you please show, with what parameters you've called that method, what you get and what you've expected to get?Allocate
And also you don't need to create that method by yourself, there is already same method in Convert class, only difference it's string parameter is object type.Allocate
Input (string): 465.09, expected Output (double): 465.09. But the result is 46509...Yalu
I know, i implemented it for testing purposes only.Yalu
what is your doubleValue again, cause I'm testing it and result is 465.09.Allocate
Let us continue this discussion in chat.Allocate
Y
1

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.

Yalu answered 12/3, 2018 at 11:41 Comment(0)
S
9

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.

Speos answered 5/10, 2017 at 6:48 Comment(0)
B
2

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.

Bigner answered 5/10, 2017 at 6:52 Comment(2)
Is it possible to "sync" the culture on my machines? The point is, it's from the same windows image... Also, please see my comment in the question...Yalu
I don't think it would be possible to sync the culture of all your machines unless you do them manually, but anyways what I would prefer you to use is double.Parse(str, new CultureInfo("yourCulture")); as you will always know the required data will be in which format and you can easily format it.Bigner
Y
1

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.

Yalu answered 12/3, 2018 at 11:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.