DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Convert DMS to Dec

1. Registered User
Join Date
Oct 2008
Posts
15

## Convert DMS to Dec

Hello,

This function does not work with negatives values !

Example: Convert_Decimal("25° 30' 00' ' ")--> 25.5
Convert_Decimal("-25° 30' 00' ' ")--> 24.5
Need help.

Thank's

Code:
```Function Convert_Decimal(degree_deg As String) As Double
' Declare the variables to be double precision floating-point.
Dim degrees As Double
Dim minutes As Double
Dim seconds As Double
' Set degree to value before "°" of Argument Passed.
degrees = Val(Left(degree_deg, InStr(1, degree_deg, "°") - 1))
' Set minutes to the value between the "°" and the "'"
' of the text string for the variable Degree_Deg divided by
' 60. The Val function converts the text string to a number.
minutes = Val(Mid(degree_deg, InStr(1, degree_deg, "°") + 1, _
InStr(1, degree_deg, "'") - InStr(1, degree_deg, "°") - 1)) / 60
' Set seconds to the number to the right of "'" that is
' converted to a value and then divided by 3600.
seconds = Val(Mid(degree_deg, InStr(1, degree_deg, "'") + _
1, Len(degree_deg) - InStr(1, degree_deg, "'") - 1)) / 3600
Convert_Decimal = degrees + minutes + seconds
End Function```

2. Try this:
Code:
```Function Convert_Decimal(degree_deg As String) As Double
' Declare the variables to be double precision floating-point.
Dim degrees As Double
Dim minutes As Double
Dim seconds As Double
Dim sign As Long
sign = IIf(Left\$(degree_deg, 1) = "-", -1, 1)
' Set degree to value before "&#176;" of Argument Passed.
degrees = Abs(Val(Left(degree_deg, InStr(1, degree_deg, "&#176;") - 1)))
' Set minutes to the value between the "&#176;" and the "'"
' of the text string for the variable Degree_Deg divided by
' 60. The Val function converts the text string to a number.
minutes = Val(Mid(degree_deg, InStr(1, degree_deg, "&#176;") + 1, _
InStr(1, degree_deg, "'") - InStr(1, degree_deg, "&#176;") - 1)) / 60
' Set seconds to the number to the right of "'" that is
' converted to a value and then divided by 3600.
seconds = Val(Mid(degree_deg, InStr(1, degree_deg, "'") + _
1, Len(degree_deg) - InStr(1, degree_deg, "'") - 1)) / 3600
Convert_Decimal = sign * (degrees + minutes + seconds)
End Function```

3. Registered User
Join Date
Oct 2008
Posts
15

## DMS to Dec

Hello mstraf,
It's perfect, Thank you very much for your help.

#### Posting Permissions

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

 FAQ Latest Articles Java .NET XML Database Enterprise