发现一个怪事,谁可以解释下(50分)

  • 主题发起人 bamboowzm
  • 开始时间
B

bamboowzm

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm1.Button1Click(Sender: TObject);
var
ff:single;
code:integer;
begin
val('0226.9'+#13,ff,code);
showmessage(floattostr(ff));
end;

运行结果是2269
好像有了个回车就不认小数点了
看来这个是Val函数的BUG
 
L

liyinwei

Unregistered / Unconfirmed
GUEST, unregistred user!
“Val函数的BUG ”???
Delphi syntax:
procedure Val(S;
var V;
var Code: Integer);
Description
......
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.
Delphi 的帮助已经说得很清楚了,如果转换不成功则把非法字符的序号赋值给 Code 参数,如果转换成功则 Code 为 0 。
检查一下 Code 的值(7)就会明白返回的 2269 是一点意义都没有的,因为转换根本就没有成功。
直接使用 Val 函数是需要自己检查转换是否成功的,一般转换用 StrToInt 或者 StrToIntDef 来处理。
 
S

sunnyboy_delphi

Unregistered / Unconfirmed
GUEST, unregistred user!
没有过 一般都是strtoint
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
567
import
I
顶部