简体   繁体   English

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

[英]Input string was not in a correct format in c# net int.parse

I have string which is like this: 我有这样的字符串:

string line = "77.139305\t28.795975"

and i seperate the two integers like this: 我将两个整数分开,如下所示:

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

whic exactly seperates the 2 numbers like this: "77.139305" and "28.795975" for lat and lng. 精确地将两个数字分开,例如:经度和经度分别为"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);
                  }

As a result it always excutes else condition and if i don't handle this exception in else condition then it gives "input string was not in a correct format in c# net int.parse" How to solve this problem ? 结果,它总是排除其他条件,如果我在其他条件下不处理此异常,则它给出“输入字符串在c#net int.parse中的格式不正确”该如何解决此问题?

You are passing in floating point numbers and using an integer parsing method. 您正在传递浮点数并使用整数解析方法。

Use the Double.Parse parsing method instead. 请改用Double.Parse解析方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 int.Parse,输入字符串的格式不正确 - int.Parse, Input string was not in a correct format 空数组上的C#Linq .Select(int.parse)导致“输入字符串格式不正确” - C# Linq .Select(int.parse) on empty array results in “Input string was not in a correct format” Int.Parse(String.Split())返回“输入字符串格式不正确”错误 - Int.Parse(String.Split()) returns “Input string was not in a correct format” error System.FormatException:输入字符串的格式不正确,来自 int.Parse - System.FormatException: Input string was not in a correct format from an int.Parse MVC,上传CSV,int.Parse(),“输入字符串格式不正确。” - MVC, uploading CSV, int.Parse(), “Input string was not in a correct format.” “输入字符串的格式不正确。”在SQL语句上调用int.Parse - “Input string was not in a correct format.” calling int.Parse on a SQL Statement C#:int.Parse无法正常工作 - c# : int.Parse not working correctly C#-int.Parse帮助吗? - C# - int.Parse help? c#中输入字符串的格式不正确,int值的格式不正确 - Input string was not in a correct format in c#, int value is not in correct format FormatException int.Parse() 即使传递正确的格式字符串值 - FormatException int.Parse() even when passing correct format string value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM