我正在使用SysUtils.Format
函数和variant
值,我发现这个函数只有在格式字符串是%s
. 我检查了有关该Format
函数的文档,但没有任何关于如何处理变量值的参考。
考虑这个简单的应用程序:
{$APPTYPE CONSOLE}
uses
Variants,
SysUtils;
procedure TestFormat;
var
v : Variant;
begin
v:=100;
writeln(Format('The VarType of v is %s',[VarTypeAsText(VarType(v))]));
writeln(Format('The value of v is %s',[v]));//ok
v:='100';
writeln(Format('The VarType of v is %s',[VarTypeAsText(VarType(v))]));
writeln(Format('The value of v is %s',[v]));//ok
v:=100;
writeln(Format('The VarType of v is %s',[VarTypeAsText(VarType(v))]));
writeln(Format('The value of v is %d',[v]));//raise a EConvertError exception EConvertError: Format '%d' invalid or incompatible with argument
end;
begin
try
TestFormat;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
readln;
end.
这是此功能的错误还是简单的限制?
我已经在 Delphi 5、Delphi 2007 和 Delphi XE 中检查过这种行为。