Click to See Complete Forum and Search --> : Converting decimal degrees to degrees minutes seconds.
k0ve
Aug 30th, 2009, 10:39 PM
Hey there,
I've got an issue converting degrees to DMS.
The whole units of degrees will remain the same (i.e. in 121.135°, start with 121°).
Multiply the decimal by 60 (i.e. .135 * 60 = 8.1).
The whole number becomes the minutes (8').
Take the remaining decimal and multiply by 60. (i.e. .1 * 60 = 6).
The resulting number becomes the seconds (6"). Seconds can remain as a decimal.
I was curious how i could take only the whole digits out of each number (i.e. 8' and 6") and apply them to a variable then continue on with the calc.
Any help would be much appreciated.
Thanks
Max Peck
Aug 31st, 2009, 10:44 AM
Simple. Create a form (I'm using VB.Net here) with a TextBox and a command button. Here's the code under the command button.
Private Sub btnToDMS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnToDMS.Click
Dim H, M, S As Integer
Dim XReg As Double
XReg = Val(txtDisplay.Text)
H = Int(XReg)
XReg -= H
XReg *= 60
M = Int(XReg)
XReg -= M
XReg *= 60
S = Int(XReg)
txtDisplay.Text = H & "." & Format(M, "00") & "." & Format(S, "00")
End Sub
Yeah, I know I'm using some pre Dot-Net constructs in the code; but they're still usable. If you really want to use TryParse instead of Val go ahead - but this will work. Put 121.135 in the text box, click the button and you'll get 121.08.06. Format it as you like.
HTH,
-Max :D
k0ve
Aug 31st, 2009, 06:34 PM
Thanks a bunch, that looks good.
But the thing is, this is going into a function for excell. can i get rid of the button command? (as you can tell im extremely new to this)
Thanks in advance
Max Peck
Aug 31st, 2009, 07:49 PM
Thanks a bunch, that looks good.
But the thing is, this is going into a function for excell. can i get rid of the button command? (as you can tell im extremely new to this)
Thanks in advance
Sure you can. I just put it into a button event for testing purposes. You might want to code it as a function instead - like this:
Private Function DDDtoDMS(ByVal text As String) As String
Dim H, M, S As Integer
Dim XReg As Double
XReg = Val(text)
H = Int(XReg)
XReg -= H
XReg *= 60
M = Int(XReg)
XReg -= M
XReg *= 60
S = Int(XReg)
Return (H & "." & Format(M, "00") & "." & Format(S, "00"))
End Function
Then you just would just pass the string to it and it would be returned as a string. Here's how I called it from the button control:
Private Sub btnToDMS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnToDMS.Click
txtDisplay.Text = DDDtoDMS(txtDisplay.Text)
End Sub
-Max :D
k0ve
Aug 31st, 2009, 08:22 PM
Thanks loads dude, i'l see how that works for me.
cheers
beez
Sep 2nd, 2009, 12:33 AM
Hi there!!
I try this code, its works!!..may i know it is decimal degrees same as radiant?
opus
Sep 2nd, 2009, 06:32 AM
That's a NO!
In Radians a full circle is 2*PI, in degrees a circle is 360 degrees.
With all your math knowledge you should be able to convert in all directions !-)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.