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

徘徊边缘 提交于 2019-12-05 08:52:11

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!