String for integer conversion in Pascal, how to do it?

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

Thanks.

+4
source share
5 answers

Val Procedure:

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

This procedure works with decimal and real numbers.

Parmeters:

  • S char sequence; for the correct conversion, it must contain '+,' - ',',., 0..9.
  • V The result of the conversion. If the result is integer, then S cannot contain ", ..
  • C Returns the character position from S that interrupts 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 
+8
source

You can use the Val function.

Example:

 var sNum: String; iNum: Integer; code: Integer; begin s := '101'; Val(s, iNum, code); end. 
+1
source

You want Val() .

+1
source

You can use this,

 var i: integer; s: string; begin str(i, s); write(i); 
+1
source
  Textval := '123'; Val(Textval, Number, Code) ---> Code = 0, Number = 123 Textval := '12345x2'; Val( Textval, Number, Code) ---> Code = 6, Number remains unchanged; 

Val (TextVal, Number, Code), which converts String to a number. if possible, the result of the code = 0, the number of the error indicator elese.

0
source

All Articles