开发者

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

开发者 https://www.devze.com 2023-01-23 21:00 出处:网络
Ho开发者_开发知识库w to convert a number printed in a string into integer? Thank you.The is procedure Val:

Ho开发者_开发知识库w to convert a number printed in a string into integer?

Thank you.


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


You can use Val function.

Example:

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

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


You want Val().


You can use like this,

var

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


 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 code = 0, elese error indication number.

0

精彩评论

暂无评论...
验证码 换一张
取 消