Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Delphi: Общие вопросы > Процедура Val


Автор: Akella 25.5.2005, 11:44
Из справки
Цитата
Converts a string to a numeric representation.

Unit

System

Category

string handling routines

Delphi syntax:

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

Description

In Delphi code, Val converts the string value S to its numeric representation, as if it were read from a text file with Read.

S is a string-type expression; it must be a sequence of characters that form a signed real number.

V is an integer-type or real-type variable. If V is an integer-type variable, S must form a whole number.

Code is a variable of type Integer.

If the string is invalid, the index of the offending character is stored in Code; otherwise, Code is set to zero. For a null-terminated string, the error position returned in Code is one larger than the actual zero-based index of the character in error.

Val performs range checking differently depending upon the setting of the $R compiler directive and the type of the parameter V.


Объясните, что делает эта процедура, зачем она нужна, и что у неё за параметры?

Автор: Yanis 25.5.2005, 11:54
Дословно: "перевод строки в число". Например в той же справке есть пример:
Код

var 
  I, Code: Integer;
begin
  { Get text from TEdit control }
  Val(Edit1.Text, I, Code);
  { Error during conversion to integer? }
  if Code <> 0 then
    MessageDlg('Error at position: ' + IntToStr(Code), mtWarning, [mbOk], 0)
  else
    Canvas.TextOut(10, 10, 'Value = ' + IntToStr(I));

Просто перевод оформлен как процедура, а не функция. Досталась от Паскаля. StrToInt основана на процедуре Val.
Добавлено @ 11:59
Цитата(dsergey @ 25.5.2005, 11:44)
S is a string-type expression;
S - выражение строкового типа.
Цитата(dsergey @ 25.5.2005, 11:44)
V is an integer-type or real-type variable.
V - переменная целого типа или натурального, т.е. вещественного.
Цитата(dsergey @ 25.5.2005, 11:44)
Code is a variable of type Integer.

Code - результат перевода строки. Т.е. если строковое выражение не корректное, то Code отличен от нуля.

Автор: Akella 25.5.2005, 14:17
Цитата
If the string is invalid, the index of the offending character is stored in Code

в ней, мне кажеться, сохраняется индекс неверного символа

Автор: Петрович 26.5.2005, 07:45
Цитата(dsergey @ 25.5.2005, 15:17)
в ней, мне кажеться, сохраняется индекс неверного символа

Да, если таковой имеется

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)