Converting decimal degrees to degrees minutes seconds.
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
Re: Converting decimal degrees to degrees minutes seconds.
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.
vb Code:
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
Re: Converting decimal degrees to degrees minutes seconds.
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
Re: Converting decimal degrees to degrees minutes seconds.
Quote:
Originally Posted by
k0ve
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:
vb Code:
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:
vb Code:
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
Re: Converting decimal degrees to degrees minutes seconds.
Thanks loads dude, i'l see how that works for me.
cheers
Re: Converting decimal degrees to degrees minutes seconds.
Hi there!!
I try this code, its works!!..may i know it is decimal degrees same as radiant?
Re: Converting decimal degrees to degrees minutes seconds.
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 !-)