6

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

Thank you.

5 Answers 5

11

This is the procedure Val:

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

This procedure operates on integer and real numbers.

Parameters:

  • S char sequence; for proper conversion, it has to contain only characters from the set +-,.0123456789.
  • V The result of conversion, if conversion succeeds. If the result is going to be an Integer, then S can't contain , or ..
  • Code Returns the position in S of the character that interrupted the conversion, if conversion does not succeed.

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
Sign up to request clarification or add additional context in comments.

Comments

1
 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.

Comments

1

Since ISO standard 10206 “Extended Pascal” the procedure readStr is built‑in into the programming language. It is equivalent in function to read/readLn in conjunction with a text file (except that no text file is needed). That means leading blanks are ignored, followed by one valid integer literal which is terminated by blanks or simply the end of string. Example:

program readStrDemo(output);
    var
        n: integer;
    begin
        readStr('  +1234 Hello world!', n);
        writeLn(n) { prints 1234 }
    end.

In contrast to Borland Pascal’s val procedure you cannot emit a user‑friendly error message, though, specifically which (first) character is causing troubles. Pascal’s built‑in read, readLn and readStr procedures simply fail, the specifics are processor‐dependent. On the other hand, read, readLn and readStr are standardized so there is no vendor lock‑in.

Note, in addition to reading decimal integer literals, read/readLn/readStr can parse any integer literal that is legal in your Pascal source code, including base‑prefixed integer literals such 16#FF (255).

Comments

0

You can use Val function.

Example:

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

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

1 Comment

This code don't work. The s must be sNum
0

You can use like this,

var

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

1 Comment

−1 This is the wrong way around. The question wants stringinteger. However, Borland Pascal’s str procedure turns numeric (and enumerated type) values into a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.