c# - 在 c# net int.parse 中,输入字符串的格式不正确

标签 c# .net parsing

我有这样的字符串:

string line = "77.139305\t28.795975"

然后我像这样分开两个整数:

string lat = line.Substring(0, 9);
string lng = line.Substring(10, 9);

这两个数字完全分开,如下所示: “77.139305”“28.795975” 用于纬度和经度。

  int latitude,longitude;
   bool result = Int32.TryParse(lat, out latitude);
                  if (result)
                  {
                      col1[i] = latitude;
                      Console.WriteLine("lat: " + col1[i]);
                  }
                  else
                  {
                      if (lat == null) lat = "";
                      Console.WriteLine("Attempted conversion of lat '{0}' failed.", lat);
                  }
                  bool result2 = Int32.TryParse(lng, out longitude);
                  if (result2)
                  {
                      col2[i] = longitude;
                      Console.WriteLine("longit: " + col2[i]);
                  }
                  else
                  {
                      if (lng == null) lng = "";
                      Console.WriteLine("Attempted conversion of lng '{0}' failed.", lng);
                  }

结果它总是执行其他条件,如果我不在其他条件下处理这个异常,那么它会给出“输入字符串在 c# net int.parse 中的格式不正确” 如何解决这个问题?

最佳答案

您正在传递 float 并使用整数解析方法。

使用 Double.Parse解析方法。

关于c# - 在 c# net int.parse 中,输入字符串的格式不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30435224/

相关文章:

c# - 使用带有 EPPlus 的模板创建和保存的 .xlsx 不可读/损坏

c# - 如何从 foreach 传递多个复选框参数

c# - 如何从枚举中的数据创建对象列表?

.net - 将 UserControl 中子元素的焦点传递给 WPF 中的父元素

c# - 将所有文件和子目录从一个文件夹移动到同一文件夹的另一个子目录

php - 使用 PHP 解析 Google 海拔高度的结果

c# - WPF:是否有资源更改触发的事件

c# - 循环 ILookup,访问值

java - 当我解析时获取一种类型的所有节点

java - Android XmlPullParser 返回 null + 属性检索查询