首页 > Linux操作系统 > Linux操作系统 > delphi四舍六入函数
function TmainForm.sswr(s:string;i:integer):string;
var str,str20,temp,temp0,temp1,r:string;
begin
str:=s;
str20:=copy(str,pos('.',str)+1,length(str));
//showmessage(str20);
temp:=copy(str20,i+1,1);
temp0:=copy(str20,i,1);
temp1:=copy(str20,i+2,length(str20));
if ((length(str20)<=i)and(length(str20)>0 )) then
begin
r:=str+'00000000000';
r:=copy(r,1,pos('.',str)+i);
end else
if (length(temp1)=0 )and (strtoint(temp)=5 ) and (strtoint(temp0)mod 2=0) then
begin
r:=copy(str,1,pos('.',str)+i);
end else
begin
if i=1 then r:=formatFloat('0.0', strtofloat(str));
if i=2 then r:=formatFloat('0.00', strtofloat(str));
if i=3 then r:=formatFloat('0.000', strtofloat(str));
if i=4 then r:=formatFloat('0.0000', strtofloat(str));
if i=5 then r:=formatFloat('0.00000', strtofloat(str));
if i=6 then r:=formatFloat('0.000000', strtofloat(str));
if i=7 then r:=formatFloat('0.0000000', strtofloat(str));
r:=r+'00000000000';
r:=copy(r,1,pos('.',r)+i);
end;
if str20=str then //整数情况
begin
if i=0 then r:=str;
if i=1 then r:=str+'.0';
if i=2 then r:=str+'.00';
if i=3 then r:=str+'.000';
if i=4 then r:=str+'.0000';
if i=5 then r:=str+'.00000';
if i=6 then r:=str+'.000000';
if i=7 then r:=str+'.0000000';
end;
if ((i=0) and (str20<>str)) then r:=floattostr(round(strtofloat(str))); //小数修约成整数
Result:=r;
end;
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/11218608/viewspace-1011004/ ,如需转载,请注明出处,否则将追究法律责任。