[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)
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
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
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!
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?
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
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).
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:
nDecDeg = 42.9999
nDecDeg = Round(nDecDeg * 3600)
nDeg = Int(nDecDeg / 3600)
nMin = Int((nDecDeg - nDeg * 3600) / 60)
nsec = Int(nDecDeg - nDeg * 3600 - nMin * 60)
txtDeg = Format(nDeg, "00") + " deg., " _
& Format(nMin, "00") + " min., " _
& Format(nsec, "00") + " sec. "
MsgBox txtDeg
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
Re: How to Convert Decimal Degrees (DD) to Degrees-Minutes-Seconds (DMS)
Damm it, I missed the negative values:
vb Code:
ndecdeg = -42.999
ndecdeg = Round(ndecdeg * 3600)
nDeg = Fix(ndecdeg / 3600)
ndecdeg = Abs(ndecdeg - nDeg * 3600)
nMin = Fix(ndecdeg / 60)
nsec = Fix(ndecdeg - nMin * 60)
txtDeg = Format(nDeg, "00") + " deg., " _
& Format(nMin, "00") + " min., " _
& Format(nsec, "00") + " sec. "
MsgBox txtDeg
Should do it.
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, "°nn'ss\""")
End Function
Re: How to Convert Decimal Degrees (DD) to Degrees-Minutes-Seconds (DMS)
Matrik
Regarding ...
Quote:
Originally Posted by
matrik02
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