2

I run this part of code on my PC. The system Operating is Windows 10 Enterprise.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

static void Main(string[] args)
{
    string text_double = "0.7598";
    string message = "Using double.Parse():   ";
    try
    {
        double confidence = double.Parse(text_double);
        message += text_double + " Converted To " + confidence;
    }
    catch (Exception ex)
    {
        message += ex.Message;
    }
    Console.WriteLine(message);

    message = "Using Convert.ToDouble:   ";
    try
    {
        double confidence = Convert.ToDouble(text_double);
        message += text_double + " Converted To " + confidence;
    }
    catch (Exception ex)
    {
        message += ex.Message;
    }

    Console.WriteLine(message);
    Console.ReadKey();
    return;
}

Running The executable file on my PC returns expected results:

Using double.Parse():   0.7598 Converted To 0.7598
Using Convert.ToDouble:   0.7598 Converted To 0.7598

But when I run the executable file on my other computer running Win Server 2012 DataCenter, it always returns error as follows:

Using double.Parse():   input string was not in a correct format
Using Convert.ToDouble:   input string was not in a correct format

What is wrong with my Windows server ?

[Added]:

I realized from the comments that my Windows server has a different culture ,but I don't want to change my code when it is needed to be run on another PC. There may be other PCs with different cultures as well.

Is there any independent culture?

Rezaeimh7
  • 1,467
  • 2
  • 23
  • 40
  • 4
    Culture is different - where the decimal seperator is a `,` and not `.` – Rand Random Nov 12 '18 at 09:32
  • @RandRandom is right, you should use `InvariantCulture` and `TryParse`. – ikerbera Nov 12 '18 at 09:33
  • 2
    Possible duplicate of [Culture invariant Decimal.TryParse()](https://stackoverflow.com/questions/23131414/culture-invariant-decimal-tryparse) – mjwills Nov 12 '18 at 09:34
  • @RandRandom Changing ',' to '.' returns "0,7598 Converted To 7598" which is not true. Ran on my PC. I want the both, correct – Rezaeimh7 Nov 12 '18 at 09:37
  • 1
    @m.r226 because your PC doesn't have `,` as a decimal seperator... – Rand Random Nov 12 '18 at 09:38
  • @m.r226 - if you want both to be correct, force a specific culture while parsing your string or make your input culture independent - I can't tell you want is the better approach since you didn't explain what your situation is - to solve your current problem the only valid approach is to force an culture while parsing - as the others have already pointed out – Rand Random Nov 12 '18 at 09:42
  • @m.r266 - maybe this helps expain things - https://dotnetfiddle.net/JUKC8e – Rand Random Nov 12 '18 at 09:52
  • 1
    @m.r266 - About your update - you have to clarify what your input will be - is it always going to be this format `0.7598` - with always I mean that it never can be of any other format on any other computer? - if so then just use the `CultureInfo.InvariantCulture` - you don't have to change your code for every computer this culture `CultureInfo.InvariantCulture` is always the same on any computer and specifies a `.` as decimal seperator – Rand Random Nov 12 '18 at 10:06

2 Answers2

6

The problem is from the other computer culture settings in some countries the decimal separator is not a "." but a "," that is why you are thrown the exception.

The solution is to pass CultureInfo.InvariantCulture when parsing the value

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

static void Main(string[] args)
{
    string text_double = "0.7598";
    string message = "Using double.Parse():   ";
    try
    {
        double confidence = double.Parse(text_double, CultureInfo.InvariantCulture);
        message += text_double + " Converted To " + confidence;
    }
    catch (Exception ex)
    {
        message += ex.Message;
    }
    Console.WriteLine(message);

    message = "Using Convert.ToDouble:   ";
    try
    {
        double confidence = Convert.ToDouble(text_double, CultureInfo.InvariantCulture);
        message += text_double + " Converted To " + confidence;
    }
    catch (Exception ex)
    {
        message += ex.Message;
    }

    Console.WriteLine(message);
    Console.ReadKey();
    return;
}

References to MSDN documentation: Double.Parse Method, Convert.ToDouble Method

Givko
  • 350
  • 2
  • 14
  • In my case, the problem was about the decimal numbers, for example, the result was 1.55 on my computer but 155 on client's. Giving InvariantCulture info to the method solved the issue, Thanks a lot! – Tahirhan Jun 15 '23 at 13:34
3

That could be due to the culture on your server.

For instance with a culture fr-FR, 0.7598 isn't a valid number. While for us-EN is it.

The following will produce the same error as you described:

string text_double = "0.7598";
System.Globalization.CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");
var d = Convert.ToDouble(text_double); // <- throws

To fix that, make sure to either use the same culture or an invariant culture.

Kimserey Lam
  • 121
  • 1
  • 4
  • What if the Win server culture was not "fr-FR"? – Rezaeimh7 Nov 12 '18 at 09:49
  • 1
    @m.r226 - it doesnt have to be "fr-FR" thats just an example it can be any culture with a `,` as decimal seperator - if you don't trust us, use the calculator of windows and do the math `1/2` (`1` divided by `2`) - does it return `0,5` or `0.5` on your Windows server – Rand Random Nov 12 '18 at 09:56
  • @m.r226 `fr.FR` culture was an example of a given culture not accepting `0.7598` as a valid number. – Kimserey Lam Nov 12 '18 at 11:20