dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Convert DMS to Dec

  1. #1
    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. #2
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    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 "°" of Argument Passed.
       degrees = Abs(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 = sign * (degrees + minutes + seconds)
    End Function
    "There are two ways to write error-free programs. Only the third one works."
    Unknown

  3. #3
    Join Date
    Oct 2008
    Posts
    15

    Thumbs up DMS to Dec

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

Similar Threads

  1. Replies: 4
    Last Post: 04-22-2009, 05:38 PM
  2. convert String to Calendar
    By Force Flow in forum Java
    Replies: 4
    Last Post: 06-29-2006, 01:01 PM
  3. how to convert DEC to HEX??
    By duibye in forum VB Classic
    Replies: 7
    Last Post: 09-13-2005, 01:14 PM
  4. VB6 converting Decimal to Binary
    By LTDanNear in forum VB Classic
    Replies: 2
    Last Post: 06-11-2005, 01:22 PM
  5. SQL syntax error
    By Ted Young in forum Database
    Replies: 7
    Last Post: 08-07-2001, 03:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center