string to integer conversion in Pascal, How to do it?

41,610

Solution 1

The is procedure Val:

procedure Val(S; var V; var Code: Integer);

This procedure operate on decimal and real numbers.

Parmeters:

  • S char sequence; for proper conversion it has to contain ‘+’, ‘-‘, ‘,’, ’.’, ’0’..’9’.
  • V The result of conversion. If result going to be an Integer then S can't contain ‘,’, ’.’.
  • C Return the position of the character from S, that interrupt the conversion.

Use cases:

Var Value :Integer;

Val('1234', Value, Code);  // Value = 1234, Code = 0
Val('1.234', Value, Code); // Value = 0, Code = 2
Val('abcd', Value, Code);  // Value = 0, Code = 1

Solution 2

You can use like this,

var

i: integer;
s: string;
begin
str(i, s);
write(i);

Solution 3

You can use Val function.

Example:

var
   sNum: String;
   iNum: Integer;
   code: Integer;

begin
   s := '101';
   Val(s, iNum, code); 
end.

Solution 4

You want Val().

Share:
41,610
Aftershock
Author by

Aftershock

Updated on August 03, 2020

Comments

  • Aftershock
    Aftershock over 3 years

    How to convert a number printed in a string into integer?

    Thank you.