Home All Groups Group Topic Archive Search About

Decimal Degrees to DMS

Author
18 Aug 2006 5:52 PM
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

Author
18 Aug 2006 6:58 PM
tommaso.gastaldi
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:

Show quoteHide quote
> 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
Author
18 Aug 2006 6:59 PM
Dick Grier
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.
Author
18 Aug 2006 11:13 PM
Lance
Thanks guys.  I'll leave it as it is then.

Lance

Show quoteHide quote
"Dick Grier" <dick_grierNOSPAM@.msn.com> wrote in message
news:uTsxqhvwGHA.3964@TK2MSFTNGP04.phx.gbl...
> 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.
>
Author
20 Aug 2006 3:56 PM
Jay B. Harlow [MVP - Outlook]
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


Show quoteHide quote
"Lance" <chuckyboy81070-at-onehotpotatoimeanhotmail.com> wrote in message
news:OcEqE8uwGHA.5064@TK2MSFTNGP06.phx.gbl...
| 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
|
|
Author
21 Aug 2006 12:34 PM
Lance
That sounds interesting.  I'm  looking forward to your example.

Lance

Show quoteHide quote
"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> wrote in message
news:OjG62EHxGHA.4944@TK2MSFTNGP02.phx.gbl...
> 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
> news:OcEqE8uwGHA.5064@TK2MSFTNGP06.phx.gbl...
> | 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
> |
> |
>
>