Decimal Degrees to DMS

  • Thread starter Thread starter Lance
  • Start date Start date
L

Lance

Hi all,

Below is a funtion that converts a Lat or Lon coordinate (as a Double) to a
string of Degrees, Minutes and Seconds. It's based on an MS Exmaple for VBA
found here http://support.microsoft.com/?kbid=213449. Do you think this is
this best way to go about it .NET, or could it be done more efficiently?

/////
Private Function ConvertDegrees(ByVal DecimalDegrees As Double) As String
Dim Degrees As Int32 = 0
Dim Minutes As Single = 0
Dim Seconds As Single = 0
Dim DMS As String = ""
Degrees = Fix(DecimalDegrees)
Minutes = (DecimalDegrees - Degrees) * 60
Seconds = (Minutes - Fix(Minutes)) * 60
DMS = System.Math.Abs(Degrees) & Chr(186) & _
" " & Format(System.Math.Abs(Fix(Minutes)), "00") & _
"' " & Format(System.Math.Abs(Seconds), "00.00") & Chr(34)
Return DMS
End Function
/////

Thanks,
Lance
 
Well it depends in what context you use it.
You could perhaps remove some formatting if not needed.
They are the most time eating (in relative terms).

tommaso

Lance ha scritto:
 
Hi,

Efficiency wise, this should be OK. Unless you are doing this calculation
hundreds of times per second... It shouldn't matter. Perhaps you could make
it slightly faster (though perhaps not), but... Why bother?

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 
Lance,
When working with Latitude & Longitude I would consider defining a specific
type that represents Latitude & Longitude.

This type would then have a ToString method that formatted the value as
expected.

I would consider having a ToDouble & FromDouble conversions on this new
type.

I'll see if I can come up with a sample class later...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


"Lance" <chuckyboy81070-at-onehotpotatoimeanhotmail.com> wrote in message
| Hi all,
|
| Below is a funtion that converts a Lat or Lon coordinate (as a Double) to
a
| string of Degrees, Minutes and Seconds. It's based on an MS Exmaple for
VBA
| found here http://support.microsoft.com/?kbid=213449. Do you think this
is
| this best way to go about it .NET, or could it be done more efficiently?
|
| /////
| Private Function ConvertDegrees(ByVal DecimalDegrees As Double) As String
| Dim Degrees As Int32 = 0
| Dim Minutes As Single = 0
| Dim Seconds As Single = 0
| Dim DMS As String = ""
| Degrees = Fix(DecimalDegrees)
| Minutes = (DecimalDegrees - Degrees) * 60
| Seconds = (Minutes - Fix(Minutes)) * 60
| DMS = System.Math.Abs(Degrees) & Chr(186) & _
| " " & Format(System.Math.Abs(Fix(Minutes)), "00") & _
| "' " & Format(System.Math.Abs(Seconds), "00.00") & Chr(34)
| Return DMS
| End Function
| /////
|
| Thanks,
| Lance
|
|
 
Back
Top