如何创建一个C#控制台应用程序网格,按Latitude和Longitude对CSV条目进行分组
本文关键字:CSV Latitude Longitude 网格 创建 何创建 一个 应用程序 控制台 | 更新日期: 2023-09-27 18:20:22
我正在编写一个C#控制台应用程序,该应用程序被用作与土地使用有关的更大项目的一部分。我的控制台应用程序需要使用每个条目的纬度和经度值对CSV文件中的条目进行分组。网格分组必须具有.002度的特异性。
我发现了一些样本代码,可以做一些类似的事情,但不符合所需的特异性:
foreach (string[] row in reader) {
lat = Decimal.Parse(row[latIndex]);
lng = Decimal.Parse(row[lngIndex]);
//TODO: do math to allow for percision of .002 rather than .001 like
I'm doing here
gridID = (Math.Round(lat, 3) * 10000) + Math.Round(lng, 3);
if (!grids.TryGetValue(gridID, out totals)) {
totals = new ALUGridTotals() {
lat = lat,
lng = lng
};
grids.Add(gridID, totals);
}
本质上,我的问题是如何修改gridID语句中的Math方法以使用.002度的特异性?
非常感谢!
也许这个:
private static double WeirdRounding(double n)
{
int temp = (int)(Math.Round(n, 3) * 1000);
return temp % 2 == 0 ? (double)temp / 1000 : ((double)temp + 1) / 1000;
}
如果没有"持平",我会把它凑起来
编辑:换成int,更正了愚蠢的打字错误。