var str : string; i : integer; begin str := Trim(Edit1.Text); i := 0; while Pos('.', str) > 0 do begin Inc(i); Delete(str, 1, Pos('.', str)); end; if i = 1 then ShowMessage('=1') else ShowMessage('>1'); end;
if frac(5.1) >0.09 then
if Length(FloatToStr(Frac(1.222))) > 3 then ShowMessage('ERROR');
笨一些的方法:f: float s: string iPos: Integer; s := FloatToStr(f);iPos := Pos('.', s); s := copy(s, iPos + 1, Length(s) - iPos - 1); if Length(s) <> 1 then ....... 可以吗
function SameValue(const A, B: Single; Epsilon: Single = 0): Boolean; overload; function SameValue(const A, B: Double; Epsilon: Double = 0): Boolean; overload; function SameValue(const A, B: Extended; Epsilon: Extended = 0): Boolean; overload;
if str[length(str)-1]='.' then //符合条件 else //不符合条件或者用pos函数也是可以的
var i:double; begin edit1.Text:=floattostr((strtofloat(trim(edit1.Text))*10)); edit1.Text:=edit1.Text+'.1'; try i:=strtofloat(trim(edit1.Text)); showmessage('1位'); except showmessage('不是1位'); end; end; 绝对没问题
var i:double; begin if Frac(strtofloat(trim(edit1.Text)))=0 then begin showmessage('整数'); exit; end; edit1.Text:=floattostr((strtofloat(trim(edit1.Text))*10)); edit1.Text:=edit1.Text+'.1'; try i:=strtofloat(trim(edit1.Text)); showmessage('1位小数'); except showmessage('多位小数'); end; end; 这样就OK了
procedure TForm1.Button1Click(Sender: TObject); var i:integer; begin i:=2; while i>0 do begin label1.Caption:=inttostr(i); label2.Caption:=floattostr((1/(i-1))+1); if Frac(((1/(i-1))+1))<>0 then begin showmessage(inttostr(i)+','+floattostr(((1/(i-1))+1))); end; i:=i+1; end; end; 自己算吧
要是不知道输入的数的情况下可以用:str[length(str)-1]='.' then 要是确定了的所有的数使用统一方案就可以用:str:=format('%8.1F',1.32332); 你看看,比较一下,我觉得这两种最简单一些.
str : string;
i : integer;
begin
str := Trim(Edit1.Text);
i := 0;
while Pos('.', str) > 0 do
begin
Inc(i);
Delete(str, 1, Pos('.', str));
end;
if i = 1 then
ShowMessage('=1')
else ShowMessage('>1');
end;
ShowMessage('ERROR');
s: string
iPos: Integer;
s := FloatToStr(f);iPos := Pos('.', s);
s := copy(s, iPos + 1, Length(s) - iPos - 1);
if Length(s) <> 1 then
.......
可以吗
function SameValue(const A, B: Double; Epsilon: Double = 0): Boolean; overload;
function SameValue(const A, B: Extended; Epsilon: Extended = 0): Boolean; overload;
//符合条件
else
//不符合条件或者用pos函数也是可以的
直接
str:=format('%8.1F',1.32332);
好像永远可以符合要求吧!
begin
edit1.Text:=floattostr((strtofloat(trim(edit1.Text))*10));
edit1.Text:=edit1.Text+'.1';
try
i:=strtofloat(trim(edit1.Text));
showmessage('1位');
except
showmessage('不是1位');
end;
end;
绝对没问题
begin
if Frac(strtofloat(trim(edit1.Text)))=0 then
begin
showmessage('整数');
exit;
end;
edit1.Text:=floattostr((strtofloat(trim(edit1.Text))*10));
edit1.Text:=edit1.Text+'.1';
try
i:=strtofloat(trim(edit1.Text));
showmessage('1位小数');
except
showmessage('多位小数');
end;
end;
这样就OK了
Format('%.1f'[strtofloat(Edit1.Text)]);
在Delphi7下测试没问题!
var i:integer;
begin
i:=2;
while i>0 do
begin
label1.Caption:=inttostr(i);
label2.Caption:=floattostr((1/(i-1))+1);
if Frac(((1/(i-1))+1))<>0 then
begin
showmessage(inttostr(i)+','+floattostr(((1/(i-1))+1)));
end;
i:=i+1;
end;
end;
自己算吧
要是确定了的所有的数使用统一方案就可以用:str:=format('%8.1F',1.32332);
你看看,比较一下,我觉得这两种最简单一些.
length(str)-pos('.',str)=length(str) 整数
length(str)-pos('.',str)=1 一位小数
length(str)-pos('.',str)=2 两位小数
///bug
endif