# การแปลงพิกัดละติจูด และลองจิจูด

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp5
{
class Program
{
static void Main(string[] args)
{
double lat = 38.89861;
Console.WriteLine("{0} > {1}", lat, ConvertDoubleToDegreeAngleLat(lat));
double lon = -76.96222;
Console.WriteLine("{0} > {1}", lon, ConvertDoubleToDegreeAngleLon(lon));

double degrees;
double minutes;
double seconds;
string dir;

Console.WriteLine();

degrees = 38;
minutes = 53;
seconds = 55.0;
dir = "N";
Console.WriteLine("{0} {1} {2} {3} > {4:f5}", degrees, minutes, seconds, dir, ConvertDegreeAngleToDouble(degrees, minutes, seconds, dir));
degrees = 76;
minutes = 57;
seconds = 44.0;
dir = "W";
Console.WriteLine("{0} {1} {2} {3} > {4:f5}", degrees, minutes, seconds, dir, ConvertDegreeAngleToDouble(degrees, minutes, seconds, dir));
}

public static string ConvertDoubleToDegreeAngleLat(double doubleDegrees)
{
string dir = (doubleDegrees >= 0 ? "N" : "S");
return ConvertDoubleToDegreeAngle(doubleDegrees, dir);
}
public static string ConvertDoubleToDegreeAngleLon(double doubleDegrees)
{
string dir = (doubleDegrees >= 0 ? "E" : "W");
return ConvertDoubleToDegreeAngle(doubleDegrees, dir);
}
public static string ConvertDoubleToDegreeAngle(double doubleDegrees, string dir)
{
doubleDegrees = Math.Abs(doubleDegrees);
double latMinPart = ((doubleDegrees - Math.Truncate(doubleDegrees) / 1) * 60);
double latSecPart = ((latMinPart - Math.Truncate(latMinPart) / 1) * 60);

double degrees = Math.Truncate(doubleDegrees);
double minutes = Math.Truncate(latMinPart);
double seconds = latSecPart;

return string.Format("{0} {1} {2} {3}", degrees, minutes, seconds.ToString("#.0"), dir);
}

public static double ConvertDegreeAngleToDouble(double degrees, double minutes, double seconds, string dir)
{
//Decimal degrees =
//   whole number of degrees,
//   plus minutes divided by 60,
//   plus seconds divided by 3600

int multiplier = (dir == "S" || dir == "W") ? -1 : 1; //handle south and west
return (degrees + (minutes / 60) + (seconds / 3600)) * multiplier;
}
}
}```