-1

Given a string "3°5'2''" I need to convert it to a decimal representation.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • And what problems are you having with this requirement that you have? – Servy Jun 04 '14 at 18:15
  • 4
    Who in the world is voting this up? What is useful here? What is showing research? – Anthony Pegram Jun 04 '14 at 18:17
  • 2
    @AlexeiLevenkov The post meets *every single* metric for downvoting, and *none* of the metrics for upvoting. – Servy Jun 04 '14 at 18:17
  • [Convert Degrees/Minutes/Seconds to Decimal Coordinates](http://stackoverflow.com/questions/3249700/convert-degrees-minutes-seconds-to-decimal-coordinates) – Ulugbek Umirov Jun 04 '14 at 18:17
  • I am not aware of any in-built method or library to achieve this, but first you need to split your string on `°` and `'` , then look at [this algorithm](http://stackoverflow.com/a/2056789/961113) to get your values. – Habib Jun 04 '14 at 18:17
  • 1
    Its not clear whether you are having trouble parsing, with understanding how the conversion works or something else. You have told us your goal but not your problem. – Chris Jun 04 '14 at 18:19
  • 1
    @AlexeiLevenkov The post is representative of all sorts of things that we don't want on this site. It hasn't done its research, the information is already readily accessible, making it not useful, it's not clear (there are a number of possible decimal representations of an angle). Answering it encourages the continued abuse of the site and contribution of more very low quality content *that doesn't belong here*. Encouraging this very low quality content lowers the sites standards, drives away experts, encourages answers by non-experts, and inhibits the usefulness of the site to the world. – Servy Jun 04 '14 at 18:21
  • Are you sure that your input string will have two single quotes to denote seconds, rather than one double quote as would be more conventional? – ClickRick Jun 04 '14 at 18:48
  • @Habib Think about `TimeSpan.TotalHours()`. – Andrew Morton Jun 04 '14 at 18:54
  • @AndrewMorton, I am not sure if I am following you. Do you mean `TimeSpan` could be used for parsing lat/long in degree string to double ? – Habib Jun 04 '14 at 18:56
  • @Habib Yes, a TimeSpan has hours, minutes and seconds. Put degrees in for hours. Convert to a double with the .TotalHours property. I have said too much already. – Andrew Morton Jun 04 '14 at 19:02

2 Answers2

1

The first step, obviously, is to convert your string notation to degrees, minutes, and seconds. That's simple string-parsing, so I'll leave that as an exercise.

Let's say you're going to use a Tuple for this (http://msdn.microsoft.com/en-us/library/system.tuple.aspx).

public static double GetDegreesFromDMS(Tuple<double,double,double> dms)
{
    // First, calculate total seconds.
    double seconds = (dms.Item2 * 60) + dms.Item3;
    // This makes the fraction of a degree this number / 3600
    return dms.Item1 + (seconds / 3600);
}

To call this, you would construct a Tuple with the DMS values like so:

var dms = new Tuple<double, double, double>(3, 5, 2);
var degrees = GetDegreesFromDMS(dms);

Good luck.

Jamie
  • 1,754
  • 16
  • 34
  • 1
    Interesting approach, but it seems to be an odd use case for a Tuple (of course, for me any parameter that is a tuple is kind of weird). +1 for a working solution (and because all the angry comments are getting on my nerves)... – BradleyDotNET Jun 04 '14 at 19:23
  • The reason I use a Tuple here is symmetry. I return a Tuple from the opposite function. The other options for the opposite function were a custom type or out parameters. For just this case, though, you're right that just using three parameters would have been simpler. – Jamie Jun 04 '14 at 20:06
-1

For the math portion, I will use the answer from https://stackoverflow.com/a/3249890/1783619 You could of course write your own implementation. I would create my own "Degree" class that looks like this:

public class Degree
{
    int degrees;
    int minutes;
    int seconds;

    public static Degree Parse(string input)
    {
      //Implementation below
    }

    public decimal ToDecimal()
    {
       // From https://stackoverflow.com/a/3249890/1783619
       // Modified to use floating point division since my inputs are ints.
       //Decimal degrees = 
       //   whole number of degrees, 
       //   plus minutes divided by 60, 
       //   plus seconds divided by 3600

       return degrees + (minutes/60f) + (seconds/3600f);
    }
}

In the parse function, I would split the string based on the well known delimiters and assign the class members based on the split string. Note that this function isn't very safe for bad input as is:

public static Degree Parse(string input)
{
   Degree parsedDegree = new Degree();

   string[] seperatedStrings = input.Split(new char[] {'°', '\''});
   parsedDegree.degrees = seperatedStrings[0];
   parsedDegree.minutes = seperatedStrings[1];
   parsedDegree.seconds = seperatedStrings[2];

   return parsedDegree;
}

To use it:

Degree myDegree = Degree.Parse("3°5'2''");
Decimal myDecimal = myDegree.ToDecimal();
Community
  • 1
  • 1
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • I believe it's `''`, not `"`. – Ulugbek Umirov Jun 04 '14 at 18:21
  • if `3°2"` is valid (ie degrees and seconds, no minutes) will this work? No idea if this is an expected input or not but just curious (I don't think it will but I'm not sure). – Chris Jun 04 '14 at 18:21
  • New change will not work, since it's not a `char` anymore. Actually your previous code would work, you can use just 2 delimiters, since you will get then `3`, `5`, `2`, `` set. – Ulugbek Umirov Jun 04 '14 at 18:22
  • Chris, it would break (as I mentioned the function isn't safe for bad input yet). I'm not sure if I should leave that as an exercise for the OP here... – BradleyDotNET Jun 04 '14 at 18:23
  • @UlugbekUmirov, good point. I guess he'll have to stick with " for the seconds... And you're right, I don't actually need to worry about it! I really need to read those input strings more carefully. My apologies for the mistake – BradleyDotNET Jun 04 '14 at 18:23