Re:String number to Real number
Quote
Don Crowe wrote:
>Could someone tell me how to convert a packed array of the type character that
>represents a real number to a actual variable of the type real. Of course, I
>need help with standard pascal and can not use the VAL function available with
>TP.
Though you're working in standard pascal, you may have access to standard
libraries that will handle this conversion for you. For example, VAX pascal
has a run time library available with this conversion in it.
Otherwise, you can scan the string one character at a time and convert it to
a real number. The following example converts a char array to an integer.
You can expand this to handle a real number:
type
CharArray: array[1..60] of char;
Function CharArrayToInt(A: CharArray;LenA: integer;var N: integer):
boolean;
{ Upon Entry: A = number to convert as CharArray, with spaces removed
LenA = number of characters in A
Upon Exit: N = integer
Returns TRUE if ok, FALSE if error in conversion }
label
Done;
var
I: integer;
Sign: integer;
begin
CharArrayToInt := FALSE;
Sign := 0;
N := 0;
For I := 1 to LenA do
begin
If (A[I] = '+') or (A[I] = '-') then
begin
If Sign <> 0 then goto Done;
Case A[I] of
'+': Sign := 1;
'-': Sign := -1;
end;
end
else
begin
If (A[I] < 0) or (A[I] > 9) then goto Done;
N := 10*N + Ord(A[I]) - Ord('0');
end;
end;
N := Sign*N;
CharArrayToInt := TRUE;
Done:
end;
Ron Muzzi, EE
Great Lakes Environmental Research Lab
Ann Arbor, Michigan, USA
rmu...@glerl.noaa.gov