var S: string; LResult, ErrorIndex: Integer; begin S := '123ABC'; Val(S, LResult, ErrorIndex); ShowMessage(Format('转化结果:%D, 出错位置:%D', [LResult, ErrorIndex])); end;
var S: string; LResult:single; ErrorIndex: Integer; begin S := '123.03ABC'; Val(S, LResult, ErrorIndex); ShowMessage(Format('转化结果:%f, 出错位置:%D', [LResult, ErrorIndex])); end;我把LResult设为single,但运行后LResult的结果是12303,而不是123.03
自己写算了,比如:function MyStrToFloat(const s:String):Double; var i:Integer; t:String; begin t:=''; for i:=1 to length(s) do begin if not (s[i] in [#46,#48..#57]) then begin t:=copy(s,1,i-1); break; end; end; if t='' then t:=s; Result:=StrToFloatDef(t,0); end;
function MyStrToFloat(const s:String):Double; var i:Integer; t:String; begin t:=''; for i:=1 to length(s) do begin if not (s[i] in [#46,#48..#57]) then begin t:=copy(s,1,i-1); break; end; end; if t='' then t:=s; Result:=StrToFloatDef(t,0); end;
我想结果并不是楼主所想要的
S: string;
LResult,
ErrorIndex: Integer;
begin
S := '123ABC';
Val(S, LResult, ErrorIndex);
ShowMessage(Format('转化结果:%D, 出错位置:%D', [LResult, ErrorIndex]));
end;
S: string;
LResult:single;
ErrorIndex: Integer;
begin
S := '123.03ABC';
Val(S, LResult, ErrorIndex);
ShowMessage(Format('转化结果:%f, 出错位置:%D', [LResult, ErrorIndex]));
end;我把LResult设为single,但运行后LResult的结果是12303,而不是123.03
var i:Integer;
t:String;
begin
t:='';
for i:=1 to length(s) do
begin
if not (s[i] in [#46,#48..#57]) then
begin
t:=copy(s,1,i-1);
break;
end;
end;
if t='' then t:=s;
Result:=StrToFloatDef(t,0);
end;
function MyStrToFloat(const s:String):Double;
var i:Integer;
t:String;
begin
t:='';
for i:=1 to length(s) do
begin
if not (s[i] in [#46,#48..#57]) then
begin
t:=copy(s,1,i-1);
break;
end;
end;
if t='' then t:=s;
Result:=StrToFloatDef(t,0);
end;