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

10
  • 1
    "i tried different methods ..." Including Convert.ToDouble(text,CultureInfo.InvariantCulture) ?
    – ℍ ℍ
    Oct 5, 2017 at 6:50
  • You can either change your program or take control over the Regional settings of those 10 other PCs.
    – ℍ ℍ
    Oct 5, 2017 at 6:51
  • 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...
    – Essigwurst
    Oct 6, 2017 at 8:06
  • Which "Regional Settings" do you mean? They are the same. Or am i missing something?
    – Essigwurst
    Oct 6, 2017 at 8:09
  • Can you please show, with what parameters you've called that method, what you get and what you've expected to get?
    – SᴇM
    Oct 6, 2017 at 8:29

3 Answers 3

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.

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

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...
    – Essigwurst
    Oct 6, 2017 at 8:08
  • 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.
    – Ipsit Gaur
    Oct 6, 2017 at 8:48
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.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.