我想知道我是否遗漏了某些东西......我正在按照标准的英国文化运作.
Double result = 0;
if (Double.TryParse("1,2,3", NumberStyles.Any, CultureInfo.CurrentCulture, out result))
{
Console.WriteLine(result);
}
Run Code Online (Sandbox Code Playgroud)
预期的产出将毫无意义......"1,2,3"不应该解析为双倍.不过确实如此.根据.NET 2.0 MSDN文档
AllowThousands表示数字字符串可以有组分隔符; 例如,将数百个与数千个分开.有效的组分隔符由NumberFormatInfo的NumberGroupSeparator和CurrencyGroupSeparator属性确定,每个组中的位数由NumberFormatInfo的NumberGroupSizes和CurrencyGroupSizes属性确定.
允许数千包含在NumberStyles.Any中.对于我的文化,NumberGroupSizes是3.这只是Double.Parse中的一个错误吗?似乎不太可能,但我无法发现我做错了什么......
foreach (object item in listBox1.SelectedItems)
{
string curItem = item.ToString();
var parts = curItem.Split("{}XY=, ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
var xCoord = float.Parse(parts[0]);
var yCoord = float.Parse(parts[1]);
var point = new PointF(xCoord, yCoord);
CloudEnteringAlert.pointtocolor.Add(point);
pictureBox1.Invalidate();
}
Run Code Online (Sandbox Code Playgroud)
curItem 变量包含如下值:Cloud detected at: 0.9174312 Kilometers from the coast.
我只想从值中获取0.9174312并将其设置在变量 xCoord 中。问题是它现在进行解析的方式出现错误:
Input string was not in a correct format
我猜索引不应该为零。如何从字符串中只获取浮点数?
现在这个字符串格式每次都是相同的:
第一部分: Cloud detected at: Second part: 0.9174312 and Last part: Kilometers from the coast.
但也许将来我会更改字符串格式,所以我需要在任何地方浮点数将位于字符串的中间最后或开头以仅获取浮点数。