public static GeoAngle FromDouble(double angleInDegrees) { //ensure the value will fall within the primary range [-180.0..+180.0] while (angleInDegrees < -180.0) { angleInDegrees += 360.0; } while (angleInDegrees > 180.0) { angleInDegrees -= 360.0; } var result = new GeoAngle(); //switch the value to positive result.IsNegative = angleInDegrees < 0; angleInDegrees = Math.Abs(angleInDegrees); //gets the degree result.Degrees = (int)Math.Floor(angleInDegrees); var delta = angleInDegrees - result.Degrees; //gets minutes and seconds var seconds = (int)Math.Floor(3600.0 * delta); result.Seconds = seconds % 60; result.Minutes = (int)Math.Floor(seconds / 60.0); delta = delta * 3600.0 - seconds; //gets fractions result.Milliseconds = (int)(1000.0 * delta); return(result); }
static void Main(string[] args) { Console.WriteLine(decimal_to_degree(25.139458)); Console.WriteLine(GeoAngle.FromDouble(25.139458).ToString("NS")); GeoAngle result = GeoAngle.FromDouble(25.139458); Console.WriteLine("{0}.{1}.{2}.{3}", result.Degrees, result.Minutes, result.Seconds, result.Milliseconds); Console.ReadLine(); }