Converting from Decimal degrees to Degrees Minutes Seconds tenths.

大憨熊 提交于 2019-12-28 13:37:24

问题


Is there some sample conversion code in C# to go from decimal degrees to Degrees, Minutes, Seconds, Tenth?


回答1:


I think this should do it.

double decimal_degrees; 

// set decimal_degrees value here

double minutes = (decimal_degrees - Math.Floor(decimal_degrees)) * 60.0; 
double seconds = (minutes - Math.Floor(minutes)) * 60.0;
double tenths = (seconds - Math.Floor(seconds)) * 10.0;
// get rid of fractional part
minutes = Math.Floor(minutes);
seconds = Math.Floor(seconds);
tenths = Math.Floor(tenths);



回答2:


Here is a class I made time ago.

public class SexagesimalAngle
{
    public bool IsNegative { get; set; }
    public int Degrees { get; set; }
    public int Minutes { get; set; }
    public int Seconds { get; set; }
    public int Milliseconds { get; set; }



    public static SexagesimalAngle 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 SexagesimalAngle();

        //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;
    }



    public override string ToString()
    {
        var degrees = this.IsNegative
            ? -this.Degrees
            : this.Degrees;

        return string.Format(
            "{0}° {1:00}' {2:00}\"",
            degrees,
            this.Minutes,
            this.Seconds);
    }



    public string ToString(string format)
    {
        switch (format)
        {
            case "NS":
                return string.Format(
                    "{0}° {1:00}' {2:00}\".{3:000} {4}",
                    this.Degrees,
                    this.Minutes,
                    this.Seconds,
                    this.Milliseconds,
                    this.IsNegative ? 'S' : 'N');

            case "WE":
                return string.Format(
                    "{0}° {1:00}' {2:00}\".{3:000} {4}",
                    this.Degrees,
                    this.Minutes,
                    this.Seconds,
                    this.Milliseconds,
                    this.IsNegative ? 'W' : 'E');

            default:
                throw new NotImplementedException();
        }
    }
}



回答3:


The solution here has the conversion both ways.

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;

**From Convert Degrees/Minutes/Seconds to Decimal Coordinates




回答4:


You can simply use these 2 functions :

public Tuple<int,int,int> DecimalToDegrees(decimal decimalValue)
{
 return Tuple.Create(Convert.ToInt32( decimal.Truncate(decimalValue)),Convert.ToInt32( (decimal.Truncate(Math.Abs(decimalValue)*60))%60),Convert.ToInt32( (Math.Abs(decimalValue)*3600)%60));
}

And:

public decimal DecimalToDegrees(int deg , int min , int sec)
{ //~2.3825224324453 Meters error due to accuracy
  return deg+(min/60m)+(sec/3600m);
}


来源:https://stackoverflow.com/questions/6862684/converting-from-decimal-degrees-to-degrees-minutes-seconds-tenths

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!