C# parse string “0” to integer

前端 未结 8 948
抹茶落季
抹茶落季 2021-01-18 02:58

I have a new laptop at work and code that worked earlier in the week does not work today.

The code that worked before is, simplified:

while (dr.Read         


        
8条回答
  •  执笔经年
    2021-01-18 03:51

    A possible explanation:

    Basically, the problem was the sPositiveSign value under HKEY_CURRENT_USER\Control Panel\International being set to 0, which means the positive sign is '0'. Thus, while parsing the "positive sign 0" is being cut off and then the rest of the string ("") is parsed as a number, which doesn't work of course. This also explains why int.Parse("00") wasn't a problem. Although you can't set the positive sign to '0' through the Control Panel, it's still possible to do it through the registry, causing problems. No idea how the computer of the user in the post ended up with this wrong setting...

    Better yet, what is the output of this on your machine:

    Console.WriteLine(System.Globalization.NumberFormatInfo.GetInstance(null).PositiveSign);
    

    I'm willing to bet yours prints out a 0... when mine prints out a + sign.

    I suggest checking your Control Panel > Regional and Language Options settings... if they appear normal, try changing them to something else than back to whatever language you're using (I'm assuming English).

提交回复
热议问题