public String DoubleToFormattedString(Double coordvalue, bool iswgs, ECoordsViewType viewtype) { String result = ""; int sign = Math.Sign(coordvalue); coordvalue = Math.Abs(coordvalue); if (!iswgs) { result = coordvalue.ToString(); } else { switch (viewtype) { case ECoordsViewType.Degrees: { result = String.Format(_coordsCulture, "{0:f12}°", coordvalue); break; } case ECoordsViewType.DegreesMinutes: { int degree = (int)Math.Floor(coordvalue); double minutes = (coordvalue - degree) * 60; result = String.Format(_coordsCulture, "{0}° {1:f12}'", degree, minutes); break; } case ECoordsViewType.DegreesMinutesSeconds: { double degree = (int)Math.Truncate(coordvalue); double minutes = (coordvalue - degree) * 60; int minutesint = (int)Math.Floor(minutes); double seconds = (minutes - minutesint) * 60; result = String.Format(_coordsCulture, "{0}° {1}' {2:f12}''", degree, minutesint, seconds); break; } } } if (sign < 0) { result = "-" + result; } return(result); }
public Double?FormattedStringToDouble(String formstring, bool iswgs, ECoordsViewType viewtype) { formstring = formstring.Trim(); bool makeNegativ = false; if (!String.IsNullOrEmpty(formstring)) { makeNegativ = formstring.StartsWith("-"); if (makeNegativ) { formstring = formstring.Substring(1); } } Double?result = null; bool parsed = true; if (!iswgs) { double degrees; formstring = Regex.Replace(formstring, "\\D", CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator); parsed &= Double.TryParse(formstring, out degrees); if (parsed) { result = degrees; } } else { double degrees = 0.0; double minutes = 0.0; double seconds = 0.0; switch (viewtype) { case ECoordsViewType.Degrees: { String pattern = @"^(.*)°$"; Match match = Regex.Match(formstring, pattern); string coordDegrees = match.Groups[1].Value; coordDegrees = Regex.Replace(coordDegrees, "\\D", "."); parsed &= Double.TryParse(coordDegrees, NumberStyles.Any, _coordsCulture, out degrees); break; } case ECoordsViewType.DegreesMinutes: { String pattern = @"^(.*)° (.*)'$"; Match match = Regex.Match(formstring, pattern); string coordDegrees = match.Groups[1].Value; string coordMinutes = match.Groups[2].Value; coordMinutes = Regex.Replace(coordMinutes, "\\D", "."); parsed &= Double.TryParse(coordDegrees, NumberStyles.Any, _coordsCulture, out degrees); parsed &= Double.TryParse(coordMinutes, NumberStyles.Any, _coordsCulture, out minutes); if (parsed) { degrees = Math.Truncate(degrees); } break; } case ECoordsViewType.DegreesMinutesSeconds: { String pattern = @"^(.*)° (.*)' (.*)''$"; Match match = Regex.Match(formstring, pattern); string coordDegrees = match.Groups[1].Value; string coordMinutes = match.Groups[2].Value; string coordSeconds = match.Groups[3].Value; coordSeconds = Regex.Replace(coordSeconds, "\\D", "."); parsed &= Double.TryParse(coordDegrees, NumberStyles.Any, _coordsCulture, out degrees); parsed &= Double.TryParse(coordMinutes, NumberStyles.Any, _coordsCulture, out minutes); parsed &= Double.TryParse(coordSeconds, NumberStyles.Any, _coordsCulture, out seconds); if (parsed) { degrees = Math.Truncate(degrees); minutes = Math.Truncate(minutes); } break; } } if (parsed) { double coordLimit = (CoordType == ECoordinateType.Longitude) ? 180 : 90; if (degrees < -coordLimit) { degrees = -coordLimit; } if (degrees > coordLimit) { degrees = coordLimit; } if (viewtype != ECoordsViewType.Degrees && Math.Abs(degrees) == coordLimit) { minutes = 0; seconds = 0; } else { if (minutes < 0) { minutes = 0; } if (minutes > 60) { minutes = 60; } if (seconds < 0) { seconds = 0; } if (seconds > 60) { seconds = 60; } } int sign = Math.Sign(degrees) == 0 ? 1 : Math.Sign(degrees); result = ((seconds / 60d + minutes) / 60d + Math.Abs(degrees)) * sign; } } if (makeNegativ) { result = result * (-1); } return(result); }