[英]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.