0

VS:2019 Framework:3.1

I have the following code:

  string output="";
  string[] numbers= line.Split(" ",StringSplitOptions.RemoveEmptyEntries);
  output += string.Format("{0:000.0000} ", Double.Parse(numbers[2]));
  output += string.Format("{0:000.0000}" ,Double.Parse(numbers[3]));
  output += string.Format("{0,10:0.0000}" ,Double.Parse(numbers[4]));

Where line is an input file string. This string contains decimal numbers like 255.1905. I'm writing this code on an English windows system, and it works completely fine, but when I try running the program on a Hungarian system, it throws the following exception: "input string was not in a correct form".

I'm guessing the problem lies in the decimal point, since the Hungarian language uses "," instead of ".", but I can't seem to figure out a solution.

I've tried Double.Parse(szamok[2], new System.Globalization.CultureInfo("en-US")) and Double.Parse(szamok[2], System.Globalization.CultureInfo.InvariantCulture) but they didn't help, maybe I used them wrong.

  • 2
    `maybe I used them wrong.` most likely. Parsing fails because the current thread's culture is apparently `en-US`. If you used `en-UK` you wouldn't have a problem. `InvariantCulture` is actually `en-US`. You need to either use a Windows locale that matches the expected users' input, or specify the correct culture, eg with `CultureInfo.GetCultureInfo("hu-HU"))`. Don't create a new CultureInfo on every call, that just wastes memory. – Panagiotis Kanavos Jan 31 '22 at 14:04
  • When you don't specify the `CultureInfo` explicitly,.NET actually tries to use the *user's* locale when possible. For desktop applications, the thread culture matches the current user's locale. For web apps, ASP.NET (Core and Framework) can use the browser's preferred language to set the request's culture. There are a lot of ways to override this, so you don't have to specify the culture on every format and parse operation – Panagiotis Kanavos Jan 31 '22 at 14:06
  • Does this answer your question? [Double parse with culture format](https://stackoverflow.com/questions/5109816/double-parse-with-culture-format) – Metro Smurf Jan 31 '22 at 14:08
  • @MetroSmurf not a good duplicate - the accepted answer doesn't work (it needs to be `de-DE`) and instead of constructing a new CultureInfo object on every call, it's better to use `CultureInfo.GetCultureInfo` to get a single cached instance – Panagiotis Kanavos Jan 31 '22 at 14:11
  • @PanagiotisKanavos understood, but there are other options presented such as `NumberFormatInfo`. So, maybe not a pure duplicate, but an accepted answer isn't always the canonical answer. – Metro Smurf Jan 31 '22 at 14:21
  • @MetroSmurf that's a far worse answer and simply doesn't work. What if the text also contained thousand separators? `10.123,45` would become `10.123.45`. Worse, `10.123` would be parsed as `10` instead of `10K`. That particular answer is the *failing* answer in interviews and certification tests. – Panagiotis Kanavos Jan 31 '22 at 14:25
  • Could you add the actual string that fails to your question? Because if its really `255.1905`, then `CultureInfo.InvariantCulture` should work. – Berend Jan 31 '22 at 15:03
  • @PanagiotisKanavos InvariantCulture may look like en-US in most regards, but they are not the same, see here: https://stackoverflow.com/questions/2329297/net-are-there-any-differences-between-invariantculture-and-en-us – Berend Jan 31 '22 at 15:10
  • Why do you think the exception is thrown by Parse? String.Format can also cause the same exception. – shingo Jan 31 '22 at 15:13
  • @Berend you're right, I should have used `essentially`. As you pointed out, `255.1905` should have worked so either the input is different or the machine isn't "English". If `line` comes from a delimited file, there may be other issues, eg the delimiter isn't a space, there may be empty fields or leading whitespace, resulting in the wrong "field" getting parsed – Panagiotis Kanavos Jan 31 '22 at 15:38
  • What do the actual input lines look like? `RemoveEmptyEntries` will skip empty fields and end up trying to parse the wrong field. String splitting isn't a good way to parse delimited files/data – Panagiotis Kanavos Jan 31 '22 at 15:40
  • @PanagiotisKanavos an input row looks like this: `A 5001 255.1904 88.4708 67.0316 -------- 0.0000` There can be unlimited rows but they always follow this pattern. However I can't imagine the problem is in the input scanning, since the code runs just fine on my en-US windows software, but please enlighten me if that could be the case, I'm still new to c# developing. – DomosLsl Jan 31 '22 at 16:40
  • @PanagiotisKanavos I've just tried `Double.Parse(numbers[5].Replace(".",","), CultureInfo.GetCultureInfo("hu-HU"))` and again, it ran fine on en_US, but not on my hu_HU – DomosLsl Jan 31 '22 at 16:42
  • @shingo Yes I've also thought about that, and it could just be the case, but I'm not sure how would changing system language affect how the `string.Format()` works. – DomosLsl Jan 31 '22 at 16:44
  • @shingo You are actually right, that may be the problem. I checked and `Console.WriteLine("{0,10:0.0000} ", 57.1622);` returns 57.1622 on en-US and 57,1622 on hu_HU. Knowing this, I might be able to fix the issue, will keep you posted on whether it worked or not – DomosLsl Jan 31 '22 at 17:05
  • @shingo Even though in the small example I gave you this wouldn't throw an exception, but the "line" was actually formatted the same way, witch might have been the cause of the issue. – DomosLsl Jan 31 '22 at 17:13
  • It actually solved my problem, thanks for every answer! – DomosLsl Jan 31 '22 at 18:03
  • @DomosLsl no amount of string replacements is ever going to work. Just pass the correct culture. `Double.Parse("123,4567",CultureInfo.GetCultureInfo("hu-HU"))` works. `255.1905` requires either `en-US` or the invariant culture. `Double.Parse("123.4567",CultureInfo.InvariantCulture)` also works – Panagiotis Kanavos Jan 31 '22 at 20:44
  • @DomosLsl I'll repeat it though, if your app and system are configured correctly you don't have to hard-code cultures. You still haven't explained what your project does, what kind of application it is. – Panagiotis Kanavos Jan 31 '22 at 20:47

0 Answers0