Re: Convert angular values to stringsPosted 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. 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).
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 ] [ DelphiLand FAQ ] [ Delphi Tutorials ] |
|