Reputation: 20222
Has anyone know simple short code to convert this without use additional libraries ?
Upvotes: 1
Views: 13924
Reputation: 146409
you could use timespan: (tricky but it works)
double coord = 123.312312;
var ts = TimeSpan.FromHours(Math.Abs(coord));
int degrees = Math.Sign(coord) * Math.Floor(ts.TotalHours);
int minutes = ts.Minutes;
int seconds = ts.Seconds;
Upvotes: 4
Reputation: 2552
String representation of a location, i.e. 51°09'48.2"N 10°07'28.6"E
public static string ToDMS(this Location location)
{
var (lat, lon) = (location.Latitude, location.Longitude);
var latSec = Math.Abs(lat) % 1.0 * 3600.0;
var lonSec = Math.Abs(lon) % 1.0 * 3600.0;
return FormattableString.Invariant(
$@"{Math.Abs((int)lat)}°{(int)latSec / 60}'{latSec % 60:F1}\"{(lat >= 0 ? "N" : "S")
} {Math.Abs((int)lon)}°{(int)lonSec / 60}'{lonSec % 60:F1}\"{(lon >= 0 ? "E" : "W")}");
}
You might not like this one-liner :)
public static string ToDMS(double lat, double lon) => FormattableString.Invariant($"{Math.Abs((int)lat)}°{(int)(Math.Abs(lat) % 1.0 * 60.0)}'{Math.Abs(lat) * 3600.0 % 60:F1}\"{(lat >= 0 ? "N" : "S")} {Math.Abs((int)lon)}°{(int)(Math.Abs(lon) % 1.0 * 60.0)}'{Math.Abs(lon) * 3600.0 % 60:F1}\"{(lon >= 0 ? "E" : "W")}" );
Suggestions for any improvements welcome...
Upvotes: 0
Reputation: 48949
I came up with the following. It correctly handles negative coordinates (south latitude or west longitude) and returns the remainder (in degrees) that was not evely divided into minutes or seconds.
public static double ConvertDecimalToDegMinSec(double value, out int deg, out int min, out int sec)
{
deg = (int)value;
value = Math.Abs(value - deg);
min = (int)(value * 60);
value = value - (double)min / 60;
sec = (int)(value * 3600);
value = value - (double)sec / 3600;
return value;
}
Upvotes: 2
Reputation: 700152
Like this:
double coord = 59.345235;
int sec = (int)Math.Round(coord * 3600);
int deg = sec / 3600;
sec = Math.Abs(sec % 3600);
int min = sec / 60;
sec %= 60;
Edit: Added an Abs call so that it works for negative angles also.
Upvotes: 9
Reputation: 717
I am infering from your question that you want to convert from cartesian to polar coordinates.
If this is the case, the basic formulae you need are:
r = √ (x2 + y2)
θ = atan( y / x )
Where r is the distance and θ is the angle from x = 0 (about the origin)
Does this help?
Upvotes: 2