Results 1 to 12 of 12

Thread: [RESOLVED] How to Convert Decimal Degrees (DD) to Degrees-Minutes-Seconds (DMS)

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Resolved [RESOLVED] How to Convert Decimal Degrees (DD) to Degrees-Minutes-Seconds (DMS)

    How to convert decimal degrees (example: 42.36824) to 42 deg., 22 min., 06 sec. in Visual Basic

    Manual calculation

    (1) The Degrees are simply the numbers to the left of the decimal (using 42.36824 as in the example above, the degrees would be 42)
    (2) To determine the Minutes, multiply the number to the right of the decimal point by 60 (example: .36824 x 60 = 22.0944)
    (3) The Minutes are the numbers to the left of the decimal point (in this example, 22)
    (4) To determine the Seconds, multiply the number to the right of the decimal point by 60 (example: .0944 x 60 = 5.664)
    (5) The Seconds are the numbers to the left of the decimal point, rounded up (in this example, 06)

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to Convert Decimal Degrees (DD) to Degrees-Minutes-Seconds (DMS)

    in steps, same as manual calculation, just make sure to force rounding as appropriate

    if you get stuck post the problem code and someone will help
    Last edited by westconn1; Jan 30th, 2012 at 03:34 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: How to Convert Decimal Degrees (DD) to Degrees-Minutes-Seconds (DMS)

    Matrik

    As LaVolpe mentioned, you very nicely described the process
    in your "manual calculation" list of steps.

    Maybe something along these lines will give you a booster start ...
    Code:
    Dim nDecDeg as Double
    Dim nDeg as Integer
    Dim nMin as Integer
    Dim nSec as Integer
    Dim txtDeg as String
    '
    nDecDeg = 42.36824
    ' 1. deg
    nDeg = Int(nDecDeg)      ' 42
    v1 = nDecDeg - nDeg      ' .36824
    v2 = v1 * 60             ' 22.0944
    ' 2. min
    nMin = Int(v2)           ' 22
    v3 = v2 - nMin           ' .0944
    v4 = v3 * 60             ' 5.664
    ' 3. sec
    nSec = Int(v4)           ' 5
    v5 = v4 - nSec           ' .664
    ' 4. rounding
    nSec = nSec + IIf(v5 >= 0.5, 1, 0)   ' 5 + 1 = 6 ... rounded up
    ' 5. create string with leading 0's if number is < 10
    txtDeg = Format(nDeg, "00") + " deg., " _
           & Format(nMin, "00") + " min., " _
           & Format(nSec, "00") + " sec. "
    Spoo

  4. #4
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: How to Convert Decimal Degrees (DD) to Degrees-Minutes-Seconds (DMS)

    Hey Spoo, you forgot the execption for a round up when nSec was already 59, which would lead to a new Minute which could lead to a new hour!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: How to Convert Decimal Degrees (DD) to Degrees-Minutes-Seconds (DMS)

    hi Opus, what the problem with the code, I test it look fine to me. What your suggestion?

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: How to Convert Decimal Degrees (DD) to Degrees-Minutes-Seconds (DMS)

    I test the code, something problem, it show 42 deg, 59 min and 60 sec,

    Code:
    nDecDeg = 42.9999
    ' 1. deg
    nDeg = Int(nDecDeg)      ' 42
    v1 = nDecDeg - nDeg      ' .36824
    v2 = v1 * 60             ' 22.0944
    ' 2. min
    nMin = Int(v2)           ' 22
    v3 = v2 - nMin           ' .0944
    v4 = v3 * 60             ' 5.664
    ' 3. sec
    nSec = Int(v4)           ' 5
    v5 = v4 - nSec           ' .664
    ' 4. rounding
    nSec = nSec + IIf(v5 >= 0.5, 1, 0)   ' 5 + 1 = 6 ... rounded up
    ' 5. create string with leading 0's if number is < 10
    txtDeg = Format(nDeg, "00") + " deg., " _
           & Format(nMin, "00") + " min., " _
           & Format(nSec, "00") + " sec. "
    MsgBox txtDeg

  7. #7
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: How to Convert Decimal Degrees (DD) to Degrees-Minutes-Seconds (DMS)

    That what I was pointing at, at the end you need to check wether you have such rollups (i.e. when Seconds are 60 set seconds to 0 and Minute +1, then do the same for minutes and then for houres as well).
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  8. #8
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: How to Convert Decimal Degrees (DD) to Degrees-Minutes-Seconds (DMS)

    If you start the wholev from the back it should be easier.
    You could try this one:
    vb Code:
    1. nDecDeg = 42.9999
    2. nDecDeg = Round(nDecDeg * 3600)
    3. nDeg = Int(nDecDeg / 3600)
    4. nMin = Int((nDecDeg - nDeg * 3600) / 60)
    5. nsec = Int(nDecDeg - nDeg * 3600 - nMin * 60)
    6. txtDeg = Format(nDeg, "00") + " deg., " _
    7.        & Format(nMin, "00") + " min., " _
    8.        & Format(nsec, "00") + " sec. "
    9. MsgBox txtDeg
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  9. #9
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: How to Convert Decimal Degrees (DD) to Degrees-Minutes-Seconds (DMS)

    This seems to work for positive and negative values:
    Code:
    Option Explicit
    
    Private Function FmtDegAsDegMinSec(ByVal Degrees As Double) As String
        Dim DegPart As Double
        
        Degrees = Round(Degrees * 3600#) / 3600#
        DegPart = Fix(Degrees)
        FmtDegAsDegMinSec = _
            CStr(DegPart) & " deg., " _
          & Format$((Degrees - DegPart) / 24#, "NN"" min., ""SS"" sec.""")
    End Function
    
    Private Sub Command1_Click()
        MsgBox FmtDegAsDegMinSec(CDbl(Text1.Text))
    End Sub

  10. #10
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: How to Convert Decimal Degrees (DD) to Degrees-Minutes-Seconds (DMS)

    Damm it, I missed the negative values:
    vb Code:
    1. ndecdeg = -42.999
    2. ndecdeg = Round(ndecdeg * 3600)
    3. nDeg = Fix(ndecdeg / 3600)
    4. ndecdeg = Abs(ndecdeg - nDeg * 3600)
    5. nMin = Fix(ndecdeg / 60)
    6. nsec = Fix(ndecdeg - nMin * 60)
    7. txtDeg = Format(nDeg, "00") + " deg., " _
    8.        & Format(nMin, "00") + " min., " _
    9.        & Format(nsec, "00") + " sec. "
    10. MsgBox txtDeg
    Should do it.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  11. #11
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    804

    Re: How to Convert Decimal Degrees (DD) to Degrees-Minutes-Seconds (DMS)

    Another $.02
    Code:
    Function FormatDMS(ByVal Degs As Double) As String
     Dim Sign As Long
     Sign = Sgn(Degs)
     Degs = Int(Abs(Degs) * 3600 + 0.5) / 3600 '-- rounding to second
     FormatDMS = Sign * (Int(Degs)) Mod 360 & Format(Degs / 24, "&#176;nn'ss\""")
    End Function

  12. #12
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: How to Convert Decimal Degrees (DD) to Degrees-Minutes-Seconds (DMS)

    Matrik

    Regarding ...
    Quote Originally Posted by matrik02 View Post
    I test the code, something problem, it show 42 deg, 59 min and 60 sec,
    ... I should have added to my OP that it was an
    admittedly "brute force" approach, and that I had not
    tested it !!.

    Have you been able to resolve the problem?
    If not, do any of the other suggestions do the trick?

    Spoo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width