Board index » delphi » String number to Real number

String number to Real number

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.

Any help is greatly needed and appreciatted.

Regards, Don

-= Don't you hate these witty signature files? =-

 

Re:String number to Real number


In article <4ld373$...@mouse.bunt.com>,
Quote
Don Crowe <cro...@magic.bunt.com> 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.

Although the FAQ is for TP in the case of string to value conversion
there is some information on the standard pascal solution in our

 96535 Mar 30 20:19 ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip
 tsfaqp.zip Common Turbo Pascal Questions and Timo's answers

   All the best, Timo

....................................................................
Prof. Timo Salmi   Co-moderator of news:comp.archives.msdos.announce
Moderating at ftp:// & http://garbo.uwasa.fi archives  193.166.120.5
Department of Accounting and Business Finance  ; University of Vaasa
t...@uwasa.fi http://uwasa.fi/~ts BBS 961-3170972; FIN-65101,  Finland

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

Other Threads