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

Essigwurst
  • 555
  • 10
  • 24
  • 1
    "i tried different methods ..." Including `Convert.ToDouble(text,CultureInfo.InvariantCulture)` ? – H H Oct 05 '17 at 06:50
  • You can either change your program or take control over the Regional settings of those 10 other PCs. – H H Oct 05 '17 at 06: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 06 '17 at 08:06
  • Which "Regional Settings" do you mean? They are the same. Or am i missing something? – Essigwurst Oct 06 '17 at 08: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 06 '17 at 08:29
  • 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. – SᴇM Oct 06 '17 at 08:31
  • Input (string): `465.09`, expected Output (double): `465.09`. But the result is `46509`... – Essigwurst Oct 06 '17 at 08:34
  • I know, i implemented it for testing purposes only. – Essigwurst Oct 06 '17 at 08:35
  • what is your `doubleValue` again, cause I'm testing it and result is `465.09`. – SᴇM Oct 06 '17 at 08:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156083/discussion-between-sem-and-essigwurst). – SᴇM Oct 06 '17 at 09:09

3 Answers3

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.

SᴇM
  • 7,024
  • 3
  • 24
  • 41
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.

Ipsit Gaur
  • 2,872
  • 1
  • 23
  • 38
  • 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 06 '17 at 08: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 06 '17 at 08: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.

Essigwurst
  • 555
  • 10
  • 24