Re: Convert angular values to strings


[ Related Articles and Replies ] [ DelphiLand FAQ ] [ Delphi Tutorials ]

Posted by webmaster Guido on December 23, 2002 at 22:34:57:

In Reply to: Convert angular values to strings posted by p12219 on December 22, 2002 at 17:21:59:

: Recently I saw an application that would take an angular input value of 87 45 23.76 into one Edit Box and then display Degrees, Minutes and Seconds. It also displayed Degrees and decimal minutes, plus Decimal Degrees.
: I know how to enter DD.MMSS and handle it also I know how to Enter Deg Min Sec into seperate Edit Boxes and handle that but I have not been able to master the input of a number such as 34 23 12.5 into an Edit Box and then break it up into variables such as D M S and then use the variables to compute Decimal Degrees and Radians for my project. Is this a case where Strings are involved? I have not been able to find any information on this procedure because I don't know what the key words are in order to look it up and hope someone has a suggestion.
-------------

In the example below, the bulk of the code is needed for not showing too many confusing error messages to the user. That's because the user can do any of the following: enter degrees and minutes and seconds, or enter only degrees and minutes, or enter only seconds, or enter nothing.

In a real life application, we would have to do even more checks: is the number for the degrees valid (only allow digits from "0" to "9" in this part)? Are the minutes valid (only allow from "0" to "9", and smaller than 60)? Are the seconds valid (only from "0" to "9", plus a maximum of ONE decimal point, and smaller than 60).

Let's have a look at the various steps:

1. Save the text of the Edit-box in a string variable and remove any unwanted leading or trailing blanks (extra spaces would interfere with the rest of the code):
2. Check if at least something is entered; if not, report the error and jump out of the routine.
3. Extract the string containing the degrees. Remove this first part from the inputstring.
4. If there is still something left, extract the string containing the minutes. Remove also this second part from the inputstring.
5. If there is still something left, this is the string containing the seconds.
6. Convert the 3 strings to numbers.
7. From degrees, seconds and minutes, calculate "decimal degrees" (and calculate "decimal minutes", if you also want these).

procedure TForm1.btnCalculateClick(Sender: TObject);
var
  InputStr, DegStr, MinStr, SecStr: string;
  PosSpace1, PosSpace2: integer;
  D, M, S, DecDeg, DecMin: real;
begin
  InPutStr := Trim(Edit1.Text);   // remove unwanted blanks
  if Length(InputStr) < 1 then begin
    ShowMessage('Input is empty, nothing to convert');
    Exit;
  end;
  PosSpace1 := Pos(' ', InputStr); // position of first space
  if PosSpace1 > 0 then begin // if there are minutes
    DegStr := Copy(InputStr, 1, PosSpace1 - 1); // extract degrees
    System.Delete(InputStr, 1, PosSpace1);  // remove first part
    PosSpace2 := Pos(' ', InputStr);     // position of second space
    if PosSpace2 > 0 then begin    // if there are seconds
      MinStr := Copy(InputStr, 1, PosSpace2 - 1);  // extract minutes
      System.Delete(InputStr, 1, PosSpace2);  // remove second part
      SecStr := InputStr;  // seconds are in third part
    end
    else begin
      MinStr := InPutStr;  // all that is left are the minutes
      SecStr := '0';       // there are no seconds
    end;
  end
  else begin  // if there are no minutes
    DegStr := InputStr;  // degrees take up entire input string
    MinStr := '0';       // there are no minutes
    SecStr := '0';       // there are no seconds
  end;
  D := StrToFloat(DegStr);         // degrees
  M := StrToFloat(MinStr);         // minutes
  S := StrToFloat(SecStr);         // seconds
  DecDeg := D + M / 60 + S / 3600; // decimal degrees
  DecMin := M + S / 60;            // decimal minutes
  lblDeg.Caption := FloatToStr(D);
  lblMin.Caption := FloatToStr(M);
  lblSec.Caption := FloatToStr(S);
  lblDecDeg.Caption := FloatToStr(DecDeg);
  lblDecMin.Caption := 'This equals ' + FloatToStr(D) +
    ' degrees and ' + FloatToStr(DecMin) + ' decimal minutes';
end;


Related Articles and Replies:


[ Related Articles and Replies ] [ DelphiLand FAQ ] [ Delphi Tutorials ]