0

I found that CultureInfo class returns information such as number format which is different when executing in windows 8.1 and 10. I created a simple console application to conform which is given below. In windows 8.1 the result of conversion is 2235. But in windows 10 the result is 0. I am using visual studio 2013 in both environment.

        CultureInfo cultureInfo = new CultureInfo(3079);
        System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;
        System.Threading.Thread.CurrentThread.CurrentUICulture = cultureInfo;

        string value = "2.235";
        decimal test;
        decimal.TryParse(value, NumberStyles.Any, cultureInfo, out test); 

Please let me know if you need any further information to give a clear picture.

Regards Prathap

prathap
  • 3
  • 2
  • 1
    The result is not 0 but `TryParse` returned `false` because it could not parse it. Always store the result of `TryParse` in a variable (or use an `if`). – Tim Schmelter Mar 17 '17 at 10:32
  • or, of course, use just regular Parse to get an exception if you always expect it to work (and it doesn't) – Malachi Mar 17 '17 at 10:37
  • @Malachi: even if you expect it to work and not parsing is exceptional it was better to use `TryParse`. Then you can decide whether you throw a meaningful exception or do something else. Use `Parse` only if you are 100% sure that it works. – Tim Schmelter Mar 17 '17 at 10:40
  • @TimSchmelter basically agreed, thus the "you always expect it to work" emphasis on always...I personally am sloppy and don't go for 100%, but I'm not gonna go on record and say others shouldn't :) – Malachi Mar 17 '17 at 10:42

1 Answers1

0

This

CultureInfo cultureInfo = new CultureInfo(3079);

is the same as

CultureInfo cultureInfo = new CultureInfo("de-AT");

It's the austrian culture. They use comma as decimal separator but space as thousands separator(as opposed to germans who use . for thousands).

So this works (appended ,99 for clarity, you can remove it):

string value = "2 235,99";
bool valid = decimal.TryParse(value, NumberStyles.Any, new CultureInfo("de-AT"), out test);

But this format not: string value = "2.235,99"

So it has nothing to do with your OS version.

Note that you should always store the result of TryParse(bool) in a variable or use an if...else instead of using the returned out value to determine whether it could be parsed or not. Even 0 could be a valid value.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Thank you very much for your explanation. My problem is when I am executing the application in windows 8.1 the output is 2235 and in windows 10 its 0. – prathap Mar 17 '17 at 11:37
  • Well, maybe they have changed the regional settings in windows for austria. Maybe you can [change it](http://www.softwareok.com/?seite=faq-Windows-10&faq=146). – Tim Schmelter Mar 17 '17 at 11:44
  • Could you please explain what is the difference between using CultureInfo cultureInfo = new CultureInfo(3079) and var testcultureinfo = CultureInfo.GetCultureInfo(3079). Because in the first option I get my local computer regional settings configuration. But when I am using GetCultureInfor function it gives a different output. – prathap Mar 17 '17 at 13:57