Convert decimal coordinates to Degrees, Minutes & Seconds by c#

后端 未结 4 580
说谎
说谎 2021-01-07 11:12

Has anyone know simple short code to convert this without use additional libraries ?

相关标签:
4条回答
  • 2021-01-07 11:49

    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.

    0 讨论(0)
  • 2021-01-07 11:51

    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;
    
    0 讨论(0)
  • 2021-01-07 11:55

    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?

    0 讨论(0)
  • 2021-01-07 12:05

    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;
    }
    
    0 讨论(0)
提交回复
热议问题