none
convert latitude and longitude from decimal to degree RRS feed

  • Question

  • Hy,

    i have a decimal number like 35,23456834 the rapresent a latitude or longitude (in degree but with decimal form for the decimal )

    My need is to transform that number in xx° - xx' - xx'' in a variabli of string

    Someone can help me?

    thank,

    MB


    Marco Bosco

    Thursday, March 22, 2012 8:53 PM

Answers

  • Hi again,

    If you want a more accurate version then use this one instead.

    Add one Button and one TextBox to a Form.

    result = 35° - 14' - 4.44602399999994''

    35 + ( 14 X ( 1/60)) + ( 4.44602399999994 X ( 1/3600)) = 35.234568339999999983333333333333 degrees

    according to Windows Calculator.  ;)

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim decimalValue As Decimal = Convert.ToDecimal(35.23456834)
    
            Dim result As String
            result = ToDMS(decimalValue)
            TextBox1.Text = result
    
        End Sub
    
        Public Function ToDMS(ByVal aDecimal As Decimal) As String
    
            Dim returnString As String = ""
            returnString = Int(aDecimal) & "° - "
            aDecimal = aDecimal - Int(aDecimal)
            Dim minutes As Decimal
            minutes = Convert.ToDecimal(Int(aDecimal / (1 / 60)))
            aDecimal = Convert.ToDecimal(aDecimal - (minutes * (1 / 60)))
            Dim seconds As Decimal
            seconds = Convert.ToDecimal((aDecimal / (1 / 3600)))
            returnString &= minutes.ToString & "' - " & seconds.ToString & "''"
            Return returnString
    
        End Function
    
    End Class




    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.

    Thursday, March 22, 2012 9:44 PM

All replies

  • This isn't really a VB question... just more of a math question.

    Try this link though:

    http://geography.about.com/library/howto/htdegrees.htm


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, March 22, 2012 8:57 PM
    Moderator
  • Hi,

    Here is a Function to do that.  :)

    Add one Button to a Form to try this please.

    Converting back I get.>>

    35.234444444444444444444444444444 degrees

    which is 35° - 14' - 4'

    Please note: Unless some fractional seconds are used, some accuracy when converting back is lost.

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim decimalValue As Decimal = Convert.ToDecimal(35.23456834)
    
            Dim result As String
            result = ToDMS(decimalValue)
            MessageBox.Show(result)
    
        End Sub
    
        Public Function ToDMS(ByVal aDecimal As Decimal) As String
    
            Dim returnString As String = ""
            returnString = Int(aDecimal) & "° - "
            aDecimal = aDecimal - Int(aDecimal)
            Dim minutes As Decimal
            minutes = Convert.ToDecimal(Int(aDecimal / (1 / 60)))
            aDecimal = Convert.ToDecimal(aDecimal - (minutes * (1 / 60)))
            Dim seconds As Decimal
            seconds = Convert.ToDecimal(Int(aDecimal / (1 / 3600)))
            returnString &= minutes.ToString & "' - " & seconds.ToString & "''"
            Return returnString
    
        End Function
    
    End Class




    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.

    Thursday, March 22, 2012 9:17 PM
  • Hi again,

    What the above program does.>>

    1. Splits the WHOLE degrees away 1st.
    2. Takes the fractional part and divides it by one sixtieth ( 1 / 60 ) to find out the number of minutes in the angle.
    3. Subtract the whole number of angle minutes from the fractional part.
    4. What remains is what is used to find the angle seconds by dividing by ( 1 / 3600 ) for the seconds.
    5. Get the whole number of seconds.
    6. Append the minutes and seconds to the returnString for the Function in the required format.

     :)

    To convert back.>>

    Add

    WHOLE degrees + ( minutes X ( 1/60 )) + ( seconds X ( 1/3600))

    35 + ( 14 X ( 1/60)) + ( 4 X ( 1/3600)) = 35.234444444444444444444444444444 degrees.

     I hope this helps.  ;-)




    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.

    Thursday, March 22, 2012 9:27 PM
  • Hi again,

    If you want a more accurate version then use this one instead.

    Add one Button and one TextBox to a Form.

    result = 35° - 14' - 4.44602399999994''

    35 + ( 14 X ( 1/60)) + ( 4.44602399999994 X ( 1/3600)) = 35.234568339999999983333333333333 degrees

    according to Windows Calculator.  ;)

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim decimalValue As Decimal = Convert.ToDecimal(35.23456834)
    
            Dim result As String
            result = ToDMS(decimalValue)
            TextBox1.Text = result
    
        End Sub
    
        Public Function ToDMS(ByVal aDecimal As Decimal) As String
    
            Dim returnString As String = ""
            returnString = Int(aDecimal) & "° - "
            aDecimal = aDecimal - Int(aDecimal)
            Dim minutes As Decimal
            minutes = Convert.ToDecimal(Int(aDecimal / (1 / 60)))
            aDecimal = Convert.ToDecimal(aDecimal - (minutes * (1 / 60)))
            Dim seconds As Decimal
            seconds = Convert.ToDecimal((aDecimal / (1 / 3600)))
            returnString &= minutes.ToString & "' - " & seconds.ToString & "''"
            Return returnString
    
        End Function
    
    End Class




    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.

    Thursday, March 22, 2012 9:44 PM