# Longitude or Latitude Convert "Degrees Minute Second" Format to "Decimal Degree" C# Example

We may need to convert longitude or latitude values from "Degrees Minute Second" format to "Decimal Degree" format.

Example : 77 2 00.000W to -77.03333

Here is a C# example for same

``    public void ConvertDegree()        {            //"77 2 00.000W"; Sample Input from textBox1            string input = textBox1.Text.ToString();            double sd = 0.0;            double min = 0.0;            double sec = 0.0;            double deg = 0.0;            string direction = input.Substring((input.Length - 1), 1);            string sign = "";            if ((direction.ToUpper() == "S") || (direction.ToUpper() == "W"))            {                sign = "-";            }                      string[] arr = input.Split(new char[] { ' ' });            min = Convert.ToDouble(arr[1]);            string[] arr1 = arr[2].Split(new char[] { '.' });            sec = Convert.ToDouble(arr1[0]);            deg = Convert.ToDouble(arr[0]);            min = min / ((double)60);            sec = sec / ((double)3600);            sd = deg + min + sec;            if (!(string.IsNullOrEmpty(sign)))            {                sd = sd * (-1);            }            sd = Math.Round(sd, 6);            string sdnew = Convert.ToString(sd);            string sdnew1 = "";            sdnew1 = string.Format("{0:0.000000}", sd);            //EXPECTED OUTPUT -77.03333        }``

This will be very usefull while using googlemaps with c#,

Note : Calculation may not be very accurate.. but its accurate !!

Author: Sean Hillmeyer30 Jun 2009 Member Level: Bronze   Points : 2

Working with your code I discovered that you throw out the right side of the decimal in the seconds portion of the string. I can't seem to come up with a reason for doing that. Convert.ToDouble can be used to pull in the whole decimal string and gives a more accurate conversion. Hope that helps to improve your example.

Author: Phagu Mahato26 Feb 2014 Member Level: Gold   Points : 10

`` public class GeoAngle{    public bool IsNegative { get; set; }    public int Degrees { get; set; }    public int Mins { get; set; }    public int Sec { get; set; }    public int MilliSec { get; set; }    public static GeoAngle FromDouble(double ExaDegree)    {                while (ExaDegree < -180.0) ExaDegree += 360.0;        while (ExaDegree > 180.0)      ExaDegree -= 360.0;        var result = new GeoAngle();              result.IsNegative = ExaDegree < 0;        ExaDegree = Math.Abs(ExaDegree);                result.Degrees = (int)Math.Floor(ExaDegree);        var delta = ExaDegree - result.Degrees;                var Sec = (int)Math.Floor(3600.0 * delta);        result.Sec = Sec % 60;        result.Mins = (int)Math.Floor(Sec / 60.0);        delta = delta * 3600.0 - Sec;              result.MilliSec = (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.Mins,            this.Sec);    }    public string ToString(string format)    {        switch (format)        {            case "NS":                return string.Format(                    "{0}° {1:00}' {2:00}\".{3:000} {4}",                    this.Degrees,                    this.Mins,                    this.Sec,                    this.MilliSec,                    this.IsNegative ? 'S' : 'N');            case "WE":                return string.Format(                    "{0}° {1:00}' {2:00}\".{3:000} {4}",                    this.Degrees,                    this.Mins,                    this.Sec,                    this.MilliSec,                    this.IsNegative ? 'W' : 'E');            default:                throw new NotImplementedException();        }    }}``

• Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
• No HTML formatting and links to other web sites are allowed.
• This is a strictly moderated site. Absolutely no spam allowed.
• Name:
Email: