Pages

Sep 4, 2009

How to convert decimal value to Degree Mins Secs and viceversa in C#

DECIMAL TO DEGREE CONVERTION ON SERVER SIDE :

private string convertToDegrees(decimal decValue)
{
//Method to convert to Decimal values to Degrees.
int Degree = 0, Mins = 0, Secs = 0;
decimal dec_Mins = 0.0M, dec_Secs = 0.0M;
string output = null;

Degree = (int)decValue;
dec_Mins = Math.Abs(decValue - Degree) * 60;
Mins = (int)dec_Mins;
dec_Secs = Math.Abs(dec_Mins - Mins) * 60;
dec_Secs = Math.Round(dec_Secs);
Secs = (int)dec_Secs;
output = Degree + "°" + Mins + "." + Secs;
return output;
}

The above method takes decimal as input and output degree.
The below method does the vice versa

DEGREE TO DECIMAL CONVERTION ON SERVER SIDE :

public string ConvertDegToDec(string value)
{
double vDeg, vMin, vSec, vConv1;
string result = string.Empty;
// attempt to convert if valid data
string[] lines = Regex.Split(value, "ΓΈ");
string[] lines1 = Regex.Split(lines[1], "'");
string[] lines2 = Regex.Split(lines1[1], "\"");
vDeg = Double.Parse(lines[0]);
vMin = Double.Parse(lines1[0]);
vSec = Double.Parse(lines2[0]);
vConv1 = vDeg + (vMin / 60) + (vSec / 3600);
result = vConv1.ToString();
return result;
}

No comments: