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

笑着哭i 提交于 2019-12-07 05:15:31

问题


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

Thank you.


回答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



回答2:


You can use Val function.

Example:

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

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



回答3:


You want Val().




回答4:


You can use like this,

var

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



回答5:


 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.



来源:https://stackoverflow.com/questions/4105073/string-to-integer-conversion-in-pascal-how-to-do-it

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