Solved

VB.net form for converting DMS to Decimal Degrees

Posted on 2013-05-21
5
576 Views
Last Modified: 2013-05-22
I'm trying to create a simple form to convert degrees minutes seconds to decimal degrees, but I'm having trouble when converting negative numbers.  When I try to convert -92 7 6.52 I get -91.8815222 but the answer should be -92.118478.  Why is this simple formula not working?
Private Sub btnDecimal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecimal.Click

        'txtLatDecimal.Text = Math.Round(CInt(txtLatDegrees.Text) + (CInt(txtLatMin.Text) / 60) + (CDec(txtLatSec.Text) / 3600), 7)

        'txtLngDecimal.Text = Math.Round(CInt(txtLngDegrees.Text) + (CInt(txtLngMin.Text) / 60) + (CDec(txtLngSec.Text) / 3600), 7)

        txtLatDecimal.Text = Math.Round(((CDec(txtLatSec.Text) / 60) + CInt(txtLatMin.Text)) / 60 + CInt(txtLatDegrees.Text), 7)
        txtLngDecimal.Text = Math.Round(((CDec(txtLngSec.Text) / 60) + CInt(txtLngMin.Text)) / 60 + CInt(txtLngDegrees.Text), 7)

    End Sub

Open in new window

0
Comment
Question by:Nate_LR
  • 3
5 Comments
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 39185999
Hi,

You are adding positive numbers to negative, so the result is closer to zero:
6.52/60+7/60-92 results to -91.8815222.

Giannis
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 39187105
You need to determine the sign of txtLngDegrees.Text and then apply that sign to the other parts of your formula. Thus your formula would become

-6.52/60 - 7/60 - 92 = -92.118478

AW
0
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 500 total points
ID: 39187249
try this:

Dim signLng as Integer = CInt(txtLngDegrees.Text)/Abs(CInt(txtLngDegrees.Text))
Dim signLat  as Integer = CInt(txtLatDegrees.Text)/Abs(Cint(txtLatDegrees.Text))

        txtLatDecimal.Text = Math.Round((( signLat*CDec(txtLatSec.Text) / 60) + signLat*CInt(txtLatMin.Text)) / 60 + CInt(txtLatDegrees.Text), 7)
        txtLngDecimal.Text = Math.Round((signLng*(CDec(txtLngSec.Text) / 60) + signLng*CInt(txtLngMin.Text)) / 60 + CInt(txtLngDegrees.Text), 7)

AW
0
 

Author Closing Comment

by:Nate_LR
ID: 39188342
Thanks
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 39188354
Glad to be of assistance.

AW
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question