Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert Label->Caption (degrees minutes seconds to decimal equivlant) 1

Status
Not open for further replies.

Burtlancaster

Programmer
Jul 22, 2004
12
GB
I need to find a way of converting a caption in the following format 45.45.45 to its decimal equivalent which is in this case its 45.7625.

Now the formula goes:

A = degrees
B = minutes
C = seconds

A + (B * 1/60) + (C * 1/60 * 1/60)


Any help would be much appreciated!

 
I would start with getting the caption into a string:
Code:
 AnsiString TheirEntryStr = ConvertLabel->Caption;

BTW, I'm doing "off the cuff" so it isn't foolproof, plus half the fun is doing things on your own.

Next you need to break the string into parts by using the "." as a delimiter.
Code:
AnsiString DegreeStr, MinuteStr, SecondStr;
int PeriodPos = TheirEntryStr.Pos(".");
if (PeriodPos == 0)
{
    // No period entered, deal with it here
}
else
{
    DegreeStr = TheirEntryStr.SubStrring(1, PeriodPos); // Got degrees
    AnsiString Part1Str = TheirEntryStr.SubStrtring(PeriodPos, TheirEntryStr.Length) // Get rest of string

    PeriodPos = Part1Str.Pos(".");
    if (PeriodPos == 0)
    {
        // No period entered, deal with it here
    }
    else
    {
        MinuteStr = Part1Str.SubStrring(1, PeriodPos); // Got minutes
        AnsiString Part2Str = Part1Str.SubStrtring(PeriodPos, Part1Str.Length)// Rest of string

        PeriodPos = Part2Str.Pos(".");
        if (PeriodPos == 0)
        {
            // No period entered, deal with it here
        }
        else
        {
            SecondStr = Part1Str.SubStrring(1, PeriodPos); // Got seconds
         }
    }
}

Next convert strings to a number.
Code:
try
{
    double DegreesDbl = DegreeStr.ToDouble;
    double MinutesDbl = MinuteStr.ToDouble;
    double SecondsDbl = SecondStr.ToDouble;
}
catch ( ...)
{
   // Opps, can't do the conversion, handle it here
}

The rest you should know. This is a rather inelligant solution. Notice that this problem lends itself nicely to a recursive function.


James P. Cottingham
-----------------------------------------
To determine how long it will take to write and debug a program, take your best estimate, multiply that by two, add one, and convert to the next higher units.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top