|
-
Mar 1st, 2011, 11:37 AM
#1
Thread Starter
New Member
VB form to convert Decimal Degrees to DMS
Hello Friends,
I'm facing problem with a vb program, which will convert decimal degrees in degrees, minutes, seconds. Actually, I want output should be in a single text box with symbols of degrees, minutes and seconds. Please help me.
-
Mar 1st, 2011, 11:57 AM
#2
Re: VB form to convert Decimal Degrees to DMS
-
Mar 1st, 2011, 12:05 PM
#3
Member
Re: VB form to convert Decimal Degrees to DMS
Take a look here
http://support.microsoft.com/kb/213449, the function "Convert_Degree" is the one you need. The article is for VBA but the code is simple VB.
-
Mar 1st, 2011, 03:25 PM
#4
Re: VB form to convert Decimal Degrees to DMS
Another variation:
Code:
Private Function FmtDegMinSec(ByVal Degrees As Single) As String
Dim lngFormat As Long
lngFormat = Int(Degrees) * 10000
lngFormat = lngFormat + Int(Degrees * 60 Mod 60) * 100
lngFormat = lngFormat + Int(Degrees * 3600 Mod 60)
FmtDegMinSec = Format$(lngFormat, "#0° 00' 00\""")
End Function
Or just:
Code:
Private Function FmtDegMinSec(ByVal Degrees As Single) As String
FmtDegMinSec = Format$(Int(Degrees) * 10000 _
+ Int(Degrees * 60 Mod 60) * 100 _
+ Int(Degrees * 3600 Mod 60), _
"#0° 00' 00\""")
End Function
Last edited by dilettante; Mar 1st, 2011 at 03:35 PM.
-
Mar 2nd, 2011, 04:39 AM
#5
Thread Starter
New Member
Re: VB form to convert Decimal Degrees to DMS
Thanks dear. It will be very helpful for me.
-
Mar 2nd, 2011, 06:58 AM
#6
Re: VB form to convert Decimal Degrees to DMS
* dilettante's function FmtDegMinSec() has rounding down error with Int()
* Function Convert_Degree() from http://support.microsoft.com/kb/213449 gives wrong format in some cases.
Code:
Function Convert_Degree(Decimal_Deg) As Variant
With Application
'Set degree to Integer of Argument Passed
Degrees = Int(Decimal_Deg)
'Set minutes to 60 times the number to the right
'of the decimal for the variable Decimal_Deg
Minutes = (Decimal_Deg - Degrees) * 60
'Set seconds to 60 times the number to the right of the
'decimal for the variable Minute
Seconds = Format(((Minutes - Int(Minutes)) * 60), "0")
'Returns the Result of degree conversion
'(for example, 10.46 = 10~ 27 ' 36")
Convert_Degree = " " & Degrees & "° " & Int(Minutes) & "' " _
& Seconds + Chr(34)
End With
End Function
* This is my own function:
Code:
Function FormatDMS(ByVal Degs As Double) As String
Degs = Int(Degs * 3600 + 0.5) / 3600 '-- rounding to second
FormatDMS = Int(Degs) & Format(Degs / 24, "°nn'ss\""")
End Function
Code:
Sub Test()
Dim d As Double
d = 134.9998
Debug.Print "Convert_Degree("; d; ") = "; Convert_Degree(d)
Debug.Print " FmtDegMinSec("; d; ") = "; FmtDegMinSec(d)
Debug.Print " FormatDMS("; d; ") = "; FormatDMS(d)
d = 134.9999
Debug.Print "Convert_Degree("; d; ") = "; Convert_Degree(d)
Debug.Print " FmtDegMinSec("; d; ") = "; FmtDegMinSec(d)
Debug.Print " FormatDMS("; d; ") = "; FormatDMS(d)
End Sub
Convert_Degree( 134.9998 ) = 134° 59' 59"
FmtDegMinSec( 134.9998 ) = 134° 00' 59"
FormatDMS( 134.9998 ) = 134°59'59"
Convert_Degree( 134.9999 ) = 134° 59' 60"
FmtDegMinSec( 134.9999 ) = 134° 00' 00"
FormatDMS( 134.9999 ) = 135°00'00"
-
Mar 2nd, 2011, 10:33 AM
#7
Re: VB form to convert Decimal Degrees to DMS
Nice catch and nice solution.
-
Mar 13th, 2019, 08:03 PM
#8
New Member
Re: VB form to convert Decimal Degrees to DMS
 Originally Posted by anhn
* dilettante's function FmtDegMinSec() has rounding down error with Int()
* Function Convert_Degree() from http://support.microsoft.com/kb/213449 gives wrong format in some cases.
Code:
Function Convert_Degree(Decimal_Deg) As Variant
With Application
'Set degree to Integer of Argument Passed
Degrees = Int(Decimal_Deg)
'Set minutes to 60 times the number to the right
'of the decimal for the variable Decimal_Deg
Minutes = (Decimal_Deg - Degrees) * 60
'Set seconds to 60 times the number to the right of the
'decimal for the variable Minute
Seconds = Format(((Minutes - Int(Minutes)) * 60), "0")
'Returns the Result of degree conversion
'(for example, 10.46 = 10~ 27 ' 36")
Convert_Degree = " " & Degrees & "° " & Int(Minutes) & "' " _
& Seconds + Chr(34)
End With
End Function
* This is my own function:
Code:
Function FormatDMS(ByVal Degs As Double) As String
Degs = Int(Degs * 3600 + 0.5) / 3600 '-- rounding to second
FormatDMS = Int(Degs) & Format(Degs / 24, "°nn'ss\""")
End Function
Code:
Sub Test()
Dim d As Double
d = 134.9998
Debug.Print "Convert_Degree("; d; ") = "; Convert_Degree(d)
Debug.Print " FmtDegMinSec("; d; ") = "; FmtDegMinSec(d)
Debug.Print " FormatDMS("; d; ") = "; FormatDMS(d)
d = 134.9999
Debug.Print "Convert_Degree("; d; ") = "; Convert_Degree(d)
Debug.Print " FmtDegMinSec("; d; ") = "; FmtDegMinSec(d)
Debug.Print " FormatDMS("; d; ") = "; FormatDMS(d)
End Sub
Convert_Degree( 134.9998 ) = 134° 59' 59"
FmtDegMinSec( 134.9998 ) = 134° 00' 59"
FormatDMS( 134.9998 ) = 134°59'59"
Convert_Degree( 134.9999 ) = 134° 59' 60"
FmtDegMinSec( 134.9999 ) = 134° 00' 00"
FormatDMS( 134.9999 ) = 135°00'00"
anhn you are in particular AWESOME !
Nice job!
Greetings Dav
-
Mar 16th, 2019, 11:29 AM
#9
New Member
Re: VB form to convert Decimal Degrees to DMS
anhn's code is elegant, but there is a problem in it, also. Invoke it again, but supplying negative values of those magnitudes.
Code:
FormatDMS(-134.9998 ) = -135°59'59"
FormatDMS(-134.9999 ) = -135°00'00"
To handle negative values (e.g., latitude in the southern hemisphere, longitude in the western hemisphere) while still doing the simple rounding, the first steps could be to extract the sign of the parameter, and then work with the absolute value of the parameter. The Sgn and Abs functions are obvious choices there. After the basic formatted result string is built, then apply a leading negative sign as desired. (I.e., you can choose to report negative values that round to zero with a leading minus sign, as the NWS does with temperature on some web pages, or without a leading minus sign, as most people expect with zero values.)
Consider the following. It's more verbose than it needs to be, but that may help someone to understand. And it shows some options the function could offer.
Code:
Private Function FormatToDegMinSec(ByVal Degrees As Double _
, Optional ByVal FormatForMinutes As String = "00" _
, Optional ByVal FormatForSeconds As String = "00" _
) As String
Dim dstrReturnValue As String
Dim din2NumericSign As Integer
Dim ddblWorkValue As Double
Dim ddblHalfOfPrecision As Double
'---- Capture the sign of the parameter.
din2NumericSign = Sgn(Degrees)
'---- Round the absolute value of the parameter to the second.
ddblWorkValue = Abs(Degrees)
ddblHalfOfPrecision = 0.5
ddblWorkValue = Int(ddblWorkValue * 3600 + ddblHalfOfPrecision)
' -- If the result is zero...
'If ddblWorkValue = 0 Then
' You could choose to reassign:
' din2NumericSign = 0
' I choose not to do that.
'End If
'---- Do the formatting.
dstrReturnValue = CStr(Int(ddblWorkValue / 3600)) & "°"
ddblWorkValue = ddblWorkValue Mod 3600
dstrReturnValue = dstrReturnValue _
& Format$(Int(ddblWorkValue / 60), FormatForMinutes) & "'"
ddblWorkValue = ddblWorkValue Mod 60
dstrReturnValue = dstrReturnValue _
& Format$(ddblWorkValue, FormatForSeconds) & """"
' -- Conditionally apply a leading minus sign.
If din2NumericSign = -1 Then
dstrReturnValue = "-" & dstrReturnValue
End If
'---- Return.
FormatToDegMinSec = dstrReturnValue
Exit Function
End Function
-
Mar 16th, 2019, 10:38 PM
#10
Re: VB form to convert Decimal Degrees to DMS
FWIW (and to maintain the spirit of a "one-liner-function"), here's a little fix for FormatDMS:
Code:
Private Function FormatDMS(ByVal Degs As Double) As String
FormatDMS = Mid$("-", Sgn(Degs * 3600 \ 1) + 2) & Abs(Degs) * 3600 \ 3600 & Format$(Degs / 24, "°nn'ss\""")
End Function
The difference to your FormatToDegMinSec-routine is, that yours is about factor 2 slower - and it will hand out negative
signed (-0°00'00") Values for the input-range-interval [-0.00013 to 0) ... whilst the above (fixed) FormatDMS doesn't.
HTH
Olaf
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|