我有一个包含纬度和经度值的CSV文件,例如:
"25°36'55.57", "E", "45°39'12.52", "N"
任何人都有一个快速而简单的C#代码将其转换为double值?
谢谢
如果你的意思是C#代码:
结果= 25 +(36/60)+(55.57/3600)
首先,您需要使用Regex或其他一些机制解析表达式并将其拆分为单个部分.然后:
String hour = "25"; String minute = "36"; String second = "55.57"; Double result = (hour) + (minute) / 60 + (second) / 3600;
当然,根据N/S或E/S切换到翻转标志.维基百科对此有一点看法:
对于计算,西/半球的后缀由西半球的负号代替.令人困惑的是,有时也会看到对东方负面的惯例.首选的惯例 - 东方是积极的 - 与北极的右手笛卡尔坐标系统一致.然后可以将特定经度与特定纬度(通常在北半球为正)组合以在地球表面上给出精确位置.(http://en.wikipedia.org/wiki/Longitude)
感谢所有快速解答.根据amdfan的回答,我将这段代码放在一起,完成C#中的工作.
///The regular expression parser used to parse the lat/long private static Regex Parser = new Regex("^(?[-+0-9]+)[^0-9]+(? [0-9]+)[^0-9]+(? [0-9.,]+)[^0-9.,ENSW]+(? [ENSW]*)$"); /// Parses the lat lon value. /// The value. ///It must have at least 3 parts 'degrees' 'minutes' 'seconds'. If it /// has E/W and N/S this is used to change the sign. ///public static double ParseLatLonValue(string value) { // If it starts and finishes with a quote, strip them off if (value.StartsWith("\"") && value.EndsWith("\"")) { value = value.Substring(1, value.Length - 2).Replace("\"\"", "\""); } // Now parse using the regex parser Match match = Parser.Match(value); if (!match.Success) { throw new ArgumentException(string.Format(CultureInfo.CurrentUICulture, "Lat/long value of '{0}' is not recognised", value)); } // Convert - adjust the sign if necessary double deg = double.Parse(match.Groups["deg"].Value); double min = double.Parse(match.Groups["min"].Value); double sec = double.Parse(match.Groups["sec"].Value); double result = deg + (min / 60) + (sec / 3600); if (match.Groups["pos"].Success) { char ch = match.Groups["pos"].Value[0]; result = ((ch == 'S') || (ch == 'W')) ? -result : result; } return result; }