C#解析字符串"0"到整数 [英] C# parse string "0" to integer

查看:69
本文介绍了C#解析字符串"0"到整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台新的笔记本电脑正在工作,而本周早些时候可用的代码今天不可用.

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())
{
    int i = int.Parse(dr.GetString(1))
}

现在,当数据库值为0时,它将失败.有时,但不是很可靠,它将代替:

Now it fails when the database value is 0. Sometimes, but not reliably, this will work instead:

while (dr.Read())
{
    int i = Convert.ToInt32(dr["FieldName"]))
}

我想念一些愚蠢的东西吗?

Am I missing something stupid?

奇怪的是,ReSharper还出现了许多奇怪的错误,并带有与上述代码相同的错误消息:输入字符串的格式不正确."(在我什至没有加载项目之前就开始.)

Oddly enough, ReSharper is also having tons of weird errors with the same error message that I am getting with the above code: "input string was not in the correct format." (Starts before I even load a project.)

有什么想法吗?有人有SP问题吗?买机器时,我确实尝试确保所有SP都是最新的.

Any ideas? Anyone having any SP issues? I did try to make sure all my SPs were up-to-date when I got the machine.

我了解如何使用Try.Parse和错误处理.此处的代码已简化.我正在从数据库表中读取测试用例.该列只有0、1和2个值.我已经证实了.我将数据库字段放入字符串变量s中,然后尝试使用int.Parse(s)进行分解.该代码在本周早些时候有效,数据库未更改.唯一改变的是我的环境.

I understand how to use Try.Parse and error-handling. The code here is simplified. I am reading test cases from a database table. This column has only 0, 1, and 2 values. I have confirmed that. I broke this down putting the database field into a string variable s and then trying int.Parse(s). The code worked earlier this week and the database has not changed. The only thing that has changed is my environment.

为完全简化问题,此行代码引发异常(输入字符串的格式不正确"):

To completely simplify the problem, this line of code throws an exception ("input string was not in the correct format"):

 int.Parse("0");

感谢大家帮助我解决了这个问题!解决方案是强制重置我的语言设置.

Thanks to everyone for helping me resolve this issue! The solution was forcing a reset of my language settings.

推荐答案

可能的解释:

基本上,问题是下的sPositiveSign值HKEY_CURRENT_USER \ Control面板\国际设置为0,这意味着正号为"0".因此,在解析正号0被截断,然后其余的字符串(")的解析为数字,这当然是行不通的.这也解释了为什么int.Parse("00")没问题.虽然你不能通过以下方式将正号设置为"0"控制面板,仍然可以通过注册表进行操作,导致问题.不知道电脑的帖子中的用户最终以这个错误的设置...

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);

我敢打赌,当我的人打印出 + 符号时,我敢打赌你的打印出 0 ....

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).

这篇关于C#解析字符串"0"到整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆