1

The program should read the number from this file and convert it to double.

Everything works fine everywhere except for one device. On my friend's laptop, the code throws an Input string was not in a correct format exception.

At the same time, before converting, I specifically output a string that should be converted to double and this string corresponds to the format.

What could be the problem?

Code:

using System;
using System.Net;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            double newVersion = 0;
            try
            {
                string data = new WebClient().DownloadString("https://raw.githubusercontent.com/denisnumb/Keyboardpp/main/last_version");
                data = data.Replace(".", ",");
                Console.WriteLine(data);
                newVersion = Convert.ToDouble(data);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            Console.WriteLine($"{newVersion}");
            Console.ReadLine();
        }
    }
}

Output on my device:

0,92

0,92

Output on a friend's device:

0,92

Input string was not in a correct format
0
denisnumb
  • 83
  • 13
  • 3
    Are you assuming that on your friend device the comma is the separator for doubles? – Steve May 25 '22 at 09:45
  • 1
    If your friend put 0.92 it will work, this is related to localisation (culture) settings – Maytham Fahmi May 25 '22 at 09:45
  • Also, you shouldn't be converting version numbers to, well, numbers. What if the version changes to `1.0-beta` for example? – DavidG May 25 '22 at 09:46
  • His system culture maybe different. – funatparties May 25 '22 at 09:46
  • Different culture of computers. One of you has comma and the dot seperated doubles. Take a look here https://stackoverflow.com/questions/2583362/how-to-convert-string-to-double-with-proper-cultureinfo – Artur May 25 '22 at 09:46
  • A others pointed out this is related to the decimal separtor settend in the laptop localisations – Stefano Cavion May 25 '22 at 09:46
  • Don't use `Convert.ToDouble` and don't replace commas with periods (or vice versa), use `Double.TryParse` and pass the appropriate `NumberStyles` and `CultureInfo`. ...actually don't even do that, because as others have pointed out version numbers aren't doubles. Keep it strings, or if you really must use `String.Split` with integers, or if you *really* must at least use `Decimal` (and note that [`Version`](https://learn.microsoft.com/dotnet/api/system.version) is a thing). – Jeroen Mostert May 25 '22 at 09:46
  • @DavidG my code only reads the numeric version. The structure is such that if the version is less than 0, then the code automatically adds the inscription `Beta` to it, if not, then it remains just a number – denisnumb May 25 '22 at 09:50
  • 1
    I'd recommand using [Version](https://learn.microsoft.com/en-us/dotnet/api/system.version.tryparse?view=net-6.0) class. – shingo May 25 '22 at 10:26
  • You probably want that version 0.9 is less than 0.15, or? When parsed as double, it will be greater, however. – Klaus Gütter May 25 '22 at 11:38

1 Answers1

1

I have looked at your code and have the following input:

  • Use HttpClient instead of WebClient.
HttpClient client = new HttpClient();
var data = await client.GetStringAsync("https://raw.githubusercontent.com/denisnumb/Keyboardpp/main/last_version");
  • Use Tryparse instead of a convert, in this case, you can choose the type, if you really need it in double or as mentioned in the comment as Version type.
NumberFormatInfo formatWithComma = new NumberFormatInfo();
formatWithComma.NumberDecimalSeparator = ".";
double.TryParse(data, NumberStyles.Any, formatWithComma, out newVersion);

Or

//newVersion type should be Version and not double
Version.TryParse(data, out newVersion);

If you wrap it up, here is my suggestion, with double.TryParse

static async Task Main(string[] args)
{
    double newVersion = 0;
    try
    {
        HttpClient client = new HttpClient();
        var data = await client.GetStringAsync("https://raw.githubusercontent.com/denisnumb/Keyboardpp/main/last_version");

        NumberFormatInfo formatWithComma = new NumberFormatInfo();
        formatWithComma.NumberDecimalSeparator = ".";
        var parsed = double.TryParse(data, NumberStyles.Any, formatWithComma, out newVersion);

        if (parsed)
        {
            Console.WriteLine(newVersion);
        }
        else
        {
            Console.WriteLine($"some thing went wrong {data}");
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
    Console.WriteLine($"{newVersion}");
    Console.ReadLine();
}

Or with Version.TryParse

static async Task Main(string[] args)
{
    Version newVersion = null;
    try
    {
        HttpClient client = new HttpClient();
        var data = await client.GetStringAsync("https://raw.githubusercontent.com/denisnumb/Keyboardpp/main/last_version");

        var parsed = Version.TryParse(data, out newVersion);

        if (parsed)
        {
            Console.WriteLine(newVersion);
        }
        else
        {
            Console.WriteLine($"some thing went wrong {data}");
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
    Console.WriteLine($"{newVersion}");
    Console.ReadLine();
}
Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137